Variadic Functions Tutorial with Examples in Go.
Variadic Function
A variadic function is one that can be called with varying numbers of arguments. The most familiar examples are fmt.Printf and its variants.
package main
import "fmt"
func add(v...
In Python, there are 2 ways to extract all the numbers contained in a string.
Using List Comprehension
List comprehensions provide a concise way to create lists.
If you only want to extract only positive integers, try the following:
#!/usr/bin/...
In Python, there are 2 ways to delete the contents of a folder.
Using rmtree Method
The shutil.rmtree() method deletes an entire directory tree; path must point to a directory. For example,
#!/usr/bin/python3
# Import module
import os, shutil
...
In Python, there are 2 ways to capitalize the first letter of each word in a string.
Using title Method
The str.title() method returns a titlecased version of the string where words start with an uppercase character and the remaining characters are...
In Python, there are 3 ways to remove all empty strings from a list of strings.
Using filter Function
The filter(function, iterable) function constructs an iterator from those elements of iterable for which function returns true.
The following exam...
In Python, there are 2 ways to get an absolute file path.
Using abspath Function
The os.path.abspath() method returns a normalized absolutized version of the pathname path. For example,
#!/usr/bin/python3
# Import module
import os
abs = os.pat...
In Python, using the str.join function is the easiest way to concatenate a list of strings into a single string
Using str.join Function
The str.join() method returns a string which is the concatenation of the strings in iterable. A TypeError will be...
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 3 ways to reverse a string.
Using slicing
Slice notation takes the form [start:stop:step].
#!/usr/bin/python3
s = "Hello World"
print(s[::-1])
# syntax
# a[start:stop] # items start through stop-1
# a[start:] # items...
In Python, there are 2 ways to extract extension from filename.
Using os.path.splitext Method
The os.path.splitext(path) method splits the pathname path into a pair (root, ext) such that root + ext == path, and the extension, ext, is empty or begins...
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 Python, there are 3 ways to list all files of a directory.
Using os.listdir Method
Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order, and does not include the special entries '.' and ...
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...
4 ways to create multiple lines string in Python.Using triple-quotes
String literals can span multiple lines. One way is using triple-quotes: """...""" or '''...'''. End of lines are automatically included in the string, but it’s possible to pr...
Convert Slice to String in Golang.Using strings.Join Function
Converting a slice of strings to a single string is pretty easy to do in Go. The strings.Join() method is present in the standard library for this purpose, and its usage is shown below:
p...