All Go Rust Python PHP JavaScript
Chrome Dev Summit to secure your spot in workshops, office hours and learning lounges!

How to Get correct file base name in Golang

Golang path and filepath Examples. Using filepath.Base The number is the index of the last slash in the string. If you want to get the file's base name, use filepath.Base: package main import ( "fmt" "path/filepath" ) func main() { path ...
Patcher56

How to Add Two Numbers in Go

An example program using numbers. Integers And Floating Point Numbers Go has several different types to represent numbers. Generally we split numbers into two different kinds: integers and floating-point numbers. Make a main.go file containing the ...
pooriabt

How to Extract All the Numbers Contained in a String in Python

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/...
Sambhav Khandelwal

How to Split a String into a List in Python

In Python, using the str.split method is the easiest way to split a string into a list Using split Method The str.split(sep=None, maxsplit=- 1) method returns a list of the words in the string, using sep as the delimiter string. If maxsplit is given...
ada

How to Capitalize the First Letter of each Word in a String in Python

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...
Patcher56

How to Remove all Empty Strings from a List of Strings in Python

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...
Sambhav Khandelwal

How to Remove all Whitespace in a String in Python

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...
pooriabt

How to Get the Filename Without the Extension from a Path in Python

In Python, there are 2 ways to get the filename without the extension from a path. Using pathlib Module You can use .stem from pathlib to get the filename without the extension from a path, it really simple. For example, #!/usr/bin/python3 # Im...
Sambhav Khandelwal

How to Index Characters in a String in Go

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...
Patcher56

How to Extract Extension from Filename in Python

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...
Unused

How to Remove a Trailing Newline in Python

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....
Tomoki

How to Split a String in Rust

In Rust, there are 3 ways to split a string. Using split Function An iterator over substrings of this string slice, separated by characters matched by a pattern. See the following example: fn main() { let s = "abc1bcd2e"; let _split = s.sp...
ada

How to Read a File Line-by-Line in Go

In Golang, there are 2 ways to read a file line-by-line. Using bufio.Scanner Function Scanner provides a convenient interface for reading data such as a file of newline-delimited lines of text. The ones that are currently implemented are: packa...
Sambhav Khandelwal

How To Split a list into equally sized chunks in Python

In Python, there are 3 ways to split a list into equally sized chunks. Using List Comprehension List comprehensions provide a concise way to create lists. The following example: #!/usr/bin/python3 # -*- coding: utf8 -*- # initialize lst = ['a'] *...
Patcher56

How to split a string into a slice in Go

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...
Unused