In Python, there are 2 ways to make a list of alphabet characters.
Using string Module
The string.ascii_lowercase is a pre-initialized string used as string constant. In Python, string ascii_lowercase will give the lowercase letters ‘abcdefghijklmno...
In Rust, there are 2 ways to check if a string contains whitespace.
Using char::is_whitespace
char::is_whitespace returns true if the character has the Unicode White_Space property.
You can pass char::is_whitespace to .contains():
fn main() {
...
In Python, there are 3 ways to remove all whitespace in a string.
Using str.replace Function
The str.replace(x) function returns a copy of the string with all occurrences of substring old replaced by new.
See the following example:
#!/usr/bin/p...
In Python, there are 2 ways to prettyprint a JSON file.
Using json module
The json module already implements some basic pretty printing in the dump and dumps functions, with the indent parameter that specifies how many spaces to indent by:
#!/us...
In Python, there are 4 ways to remove a trailing newline.
Using rstrip Method
The str.rstrip() method returns a copy of the string with trailing characters removed. For example,
#!/usr/bin/python3
s = "\nab c\r\n"
# str.rstrip([chars])
n = s....
In Golang, there are 3 ways to trim leading and trailing white spaces of a string.
Using strings.TrimSpace Function
The easiest way to trim leading and trailing white spaces of a string in Golang. For example,
package main
import (
"fmt"
"...
In Python, there are 3 ways to read a file line-by-line.
Using For Loop
For reading lines from a file, you can loop over the file object. This is memory efficient, fast, and leads to simple code:
#!/usr/bin/python3
# -*- coding: utf8 -*-
# read a...
In Rust, there are 4 ways to create multiple lines string.Using triple-quotes
A string literal is a sequence of any Unicode characters enclosed within two U+0022 (double-quote) characters, with the exception of U+0022 itself, which must be escaped b...
3 ways to convert int64 to string in Golang.Using strconv.Itoa Function
The code snippet below shows how to convert an int to a string using the Itoa function.
package main
import (
"fmt"
"strconv"
)
func main() {
s := strconv.Itoa(100)
f...
Splitting a String into a Slice in Golang.Using strings.Split Function
Use the strings.Split function to split a string into its comma separated values.
package main
import (
"fmt"
"strings"
)
func main() {
s := "Japan,Germany"
// func S...
String Constants Best Practice.String Constants in Python
The constants defined in this module are:
String constants:
string.ascii_lettersThe concatenation of the ascii_lowercase and ascii_uppercase constants described below. This value is not locale...
Converting Json to string in Python.Decoding JSON
To convert string to json in Python, use the json.loads() function. The json.loads() is a built-in Python function that accepts a valid json string and returns a dictionary to access all elements. The...
Format a string without printing in Go.Basics
Package fmt implements formatted I/O with functions analogous to C's printf and scanf. The format 'verbs' are derived from C's but are simpler.
fmt.Sprintf
Sprintf formats according to a format specif...
Compare FILES line by line.
You can use the diff command to show differences between two files, or each corresponding file in two directories. diff outputs differences between files line by line in any of several formats, selectable by command line o...
The cd (“change directory”) command is used to change the current working directory in Linux and other Unix-like operating systems.
The cd command, also known as chdir (change directory), is a command-line shell command used to change the current wo...