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, using the ord function is the easiest way to get the ASCII value of a character as an int.
Using ord Function
The ord() function returns an integer representing the Unicode code point of that character.
To get the ASCII code of a charact...
In Rust, there are 2 ways to convert a str to a &[u8].
Using as_bytes Function
The as_bytes() method converts a string slice to a byte slice. To convert the byte slice back into a string slice, use the from_utf8 function.
See the following example...
In Golang, there are 2 ways to index characters in a string.
Using individual characters
In UTF-8, ASCII characters are single-byte corresponding to the first 128 Unicode characters. Strings behave like slices of bytes. A rune is an integer value i...
In Python, there are 3 ways to pad a numeric string with zeros to the left.
Using zfill Method
The str.zfill(width) method returns a copy of the string left filled with ASCII '0' digits to make a string of length width. For example,
#!/usr/bin/...
In Python, there are 3 ways to generate a random string.Using List Comprehension
List comprehensions provide a concise way to create lists. See the following example:
#!/usr/bin/python3
# -*- coding: utf8 -*-
# Import module
import random
impor...
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...
Correct way to define Python source code encoding.PEP 263
Python will default to ASCII as standard encoding if no other encoding hints are given.
PEP 263 defines how to declare Python source code encoding.
Using Coding
To define a source code encodin...
Fetch Internet Resources Using The urllib Package.Fetching URLs
The simplest way to use urllib.request is as follows:
#!/usr/bin/env python3
# Import datetime module
import urllib.request
with urllib.request.urlopen('https://google.com') as respon...
Python String Formatting Best Practices.Fancier Output Formatting
To use formatted string literals, begin a string with f or F before the opening quotation mark or triple quotation mark. Inside this string, you can write a Python expression between {...
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...
Extracting substrings in Go.Using the slice syntax
A slice is formed by specifying two indices, a low and high bound, separated by a colon:
// a[low : high]
s := "Hello,世界"
// e
fmt.Println(s[1:2])
fmt.Println(s[1:])
// Hello,世界
fmt.Println(s[:])
...