Tutorial by Examples

Python's string type provides many functions that act on the capitalization of a string. These include : str.casefold str.upper str.lower str.capitalize str.title str.swapcase With unicode strings (the default in Python 3), these operations are not 1:1 mappings or reversible. Most of thes...
str.split(sep=None, maxsplit=-1) str.split takes a string and returns a list of substrings of the original string. The behavior differs depending on whether the sep argument is provided or omitted. If sep isn't provided, or is None, then the splitting takes place wherever there is whitespace. Howe...
Python's str type also has a method for replacing occurences of one sub-string with another sub-string in a given string. For more demanding cases, one can use re.sub. str.replace(old, new[, count]): str.replace takes two arguments old and new containing the old sub-string which is to be replace...
Python provides string interpolation and formatting functionality through the str.format function, introduced in version 2.6 and f-strings introduced in version 3.6. Given the following variables: i = 10 f = 1.5 s = "foo" l = ['a', 1, 2] d = {'a': 1, 2: 'foo'} The following statem...
One method is available for counting the number of occurrences of a sub-string in another string, str.count. str.count(sub[, start[, end]]) str.count returns an int indicating the number of non-overlapping occurrences of the sub-string sub in another string. The optional arguments start and end ...
In order to test the beginning and ending of a given string in Python, one can use the methods str.startswith() and str.endswith(). str.startswith(prefix[, start[, end]]) As it's name implies, str.startswith is used to test whether a given string starts with the given characters in prefix. >...
Python's str type also features a number of methods that can be used to evaluate the contents of a string. These are str.isalpha, str.isdigit, str.isalnum, str.isspace. Capitalization can be tested with str.isupper, str.islower and str.istitle. str.isalpha str.isalpha takes no arguments and retu...
Python supports a translate method on the str type which allows you to specify the translation table (used for replacements) as well as any characters which should be deleted in the process. str.translate(table[, deletechars]) ParameterDescriptiontableIt is a lookup table that defines the mappin...
Three methods are provided that offer the ability to strip leading and trailing characters from a string: str.strip, str.rstrip and str.lstrip. All three methods have the same signature and all three return a new string object with unwanted characters removed. str.strip([chars]) str.strip acts o...
Comparing string in a case insensitive way seems like something that's trivial, but it's not. This section only considers unicode strings (the default in Python 3). Note that Python 2 may have subtle weaknesses relative to Python 3 - the later's unicode handling is much more complete. The first thi...
A string can be used as a separator to join a list of strings together into a single string using the join() method. For example you can create a string where each element in a list is separated by a space. >>> " ".join(["once","upon","a","tim...
Python's string module provides constants for string related operations. To use them, import the string module: >>> import string string.ascii_letters: Concatenation of ascii_lowercase and ascii_uppercase: >>> string.ascii_letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQ...
A string can reversed using the built-in reversed() function, which takes a string and returns an iterator in reverse order. >>> reversed('hello') <reversed object at 0x0000000000000000> >>> [char for char in reversed('hello')] ['o', 'l', 'l', 'e', 'h'] reversed() can ...
Python provides functions for justifying strings, enabling text padding to make aligning various strings much easier. Below is an example of str.ljust and str.rjust: interstates_lengths = { 5: (1381, 2222), 19: (63, 102), 40: (2555, 4112), 93: (189,305), } for road, length in...
The contents of files and network messages may represent encoded characters. They often need to be converted to unicode for proper display. In Python 2, you may need to convert str data to Unicode characters. The default ('', "", etc.) is an ASCII string, with any values outside of ASCII ...
Python makes it extremely intuitive to check if a string contains a given substring. Just use the in operator: >>> "foo" in "foo.baz.bar" True Note: testing an empty string will always result in True: >>> "" in "test" True

Page 1 of 1