Hello Readers, Do you know that there are so many built in methods that we can use on Strings.
Dir():
To list all the built in string methods we need to use dir() function.
-> dir(str)
Output:
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']These are the inbuilt string methods in python.Lets discuss all of them one by one.
Note: All string methods returns new values. They do not change the original string.
Method | Description |
---|---|
capitalize() | Converts the first character to upper case |
casefold() | Converts string into lower case |
center() | Returns a centered string |
count() | Returns the number of times a specified value occurs in a string |
encode() | Returns an encoded version of the string |
endswith() | Returns true if the string ends with the specified value |
expandtabs() | Sets the tab size of the string |
find() | Searches the string for a specified value and returns the position of where it was found |
format() | Formats specified values in a string |
format_map() | Formats specified values in a string |
index() | Searches the string for a specified value and returns the position of where it was found |
isalnum() | Returns True if all characters in the string are alphanumeric |
isalpha() | Returns True if all characters in the string are in the alphabet |
isdecimal() | Returns True if all characters in the string are decimals |
isdigit() | Returns True if all characters in the string are digits |
isidentifier() | Returns True if the string is an identifier |
islower() | Returns True if all characters in the string are lower case |
isnumeric() | Returns True if all characters in the string are numeric |
isprintable() | Returns True if all characters in the string are printable |
isspace() | Returns True if all characters in the string are whitespaces |
istitle() | Returns True if the string follows the rules of a title |
isupper() | Returns True if all characters in the string are upper case |
join() | Joins the elements of an iterable to the end of the string |
ljust() | Returns a left justified version of the string |
lower() | Converts a string into lower case |
lstrip() | Returns a left trim version of the string |
maketrans() | Returns a translation table to be used in translations |
partition() | Returns a tuple where the string is parted into three parts |
replace() | Returns a string where a specified value is replaced with a specified value |
rfind() | Searches the string for a specified value and returns the last position of where it was found |
rindex() | Searches the string for a specified value and returns the last position of where it was found |
rjust() | Returns a right justified version of the string |
rpartition() | Returns a tuple where the string is parted into three parts |
rsplit() | Splits the string at the specified separator, and returns a list |
rstrip() | Returns a right trim version of the string |
split() | Splits the string at the specified separator, and returns a list |
splitlines() | Splits the string at line breaks and returns a list |
startswith() | Returns true if the string starts with the specified value |
strip() | Returns a trimmed version of the string |
swapcase() | Swaps cases, lower case becomes upper case and vice versa |
title() | Converts the first character of each word to upper case |
translate() | Returns a translated string |
upper() | Converts a string into upper case |
zfill() | Fills the string with a specified number of 0 values at the beginning |
Changing the capitalization of a string
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
str.title()
str.title returns the title cased version of the string, that is, every letter in the beginning of a word is made upper case and all others are made lower case.
"this Is a 'String'".title()
# "This Is A 'String'"
str.swapcase()
str.swapcase returns a new string object in which all lower case characters are swapped to upper case and all upper case characters to lower:
"this iS A STRiNG".swapcase() #Swaps case of each character
# "THIS Is a strIng"
Usage as str class methods:
It is worth noting that these methods may be called either on string objects (as shown above) or as a class method of the str class (with an explicit call to str.upper, etc.)
str.upper("This is a 'string'")
# "THIS IS A 'STRING'"
str.translate: Translating characters in a string:
The maketrans method (str.maketrans in Python 3 and string.maketrans in Python 2) allows you to generate a translation table.
>>> translation_table = str.maketrans("aeiou", "12345")
>>> my_string = "This is a string!"
>>> translated = my_string.translate(translation_table)
'Th3s 3s 1 str3ng!'
The translate method returns a string which is a translated copy of the original string.
You can set the table argument to None if you only need to delete characters.
>>> 'this syntax is very useful'.translate(None, 'aeiou')
'ths syntx s vry sfl'
str.format and f-strings: Format values into a
string
Python provides string interpolation and formatting functionality through the str.format function.
Given the following variables:
i = 10
f = 1.5
s = "foo"
l = ['a', 1, 2]
d = {'a': 1, 2: 'foo'}
The following statements are all equivalent
"10 1.5 foo ['a', 1, 2] {'a': 1, 2: 'foo'}"
>>> "{} {} {} {} {}".format(i, f, s, l, d)
>>> str.format("{} {} {} {} {}", i, f, s, l, d)
>>> "{0} {1} {2} {3} {4}".format(i, f, s, l, d)
>>> "{0:d} {1:0.1f} {2} {3!r} {4!r}".format(i, f, s, l, d)
>>> "{i:d} {f:0.1f} {s} {l!r} {d!r}".format(i=i, f=f, s=s, l=l, d=d)
>>> f"{i} {f} {s} {l} {d}"
>>> f"{i:d} {f:0.1f} {s} {l!r} {d!r}"
String module's useful constants:
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
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
string.ascii_lowercase:
Contains all lower case ASCII characters:
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
string.ascii_uppercase:
Contains all upper case ASCII characters:
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
string.digits:
Contains all decimal digit characters:
>>> string.digits
'0123456789'
Stripping unwanted leading/trailing characters from a string:
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 on a given string and removes (strips) any leading or trailing characters contained in the argument chars; if chars is not supplied or is None, all white space characters are removed by default.
For example:
>>> " a line with leading and trailing space ".strip()
'a line with leading and trailing space'
If chars is supplied, all characters contained in it are removed from the string, which is returned. For
example:
>>> ">>> a Python prompt".strip('> ') # strips '>' character and space character
'a Python prompt'
str.rstrip([chars]) and str.lstrip([chars]):
These methods have similar semantics and arguments with str.strip(), their difference lies in the direction from which they start. str.rstrip() starts from the end of the string while str.lstrip() splits from the start of the string.
For example, using str.rstrip:
>>> " spacious string ".rstrip()
' spacious string'
While, using str.lstrip:
>>> " spacious string ".rstrip()
'spacious string '
Reversing a string:
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']
Split a string based on a delimiter into a list of strings:
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. However, leading and trailing whitespace is ignored, and multiple consecutive whitespace characters are treated the same as a single whitespace character:
>>> "This is a sentence.".split()
['This', 'is', 'a', 'sentence.']
>>> " This is a sentence. ".split()
['This', 'is', 'a', 'sentence.']
>>> " ".split()
[]
The sep parameter can be used to define a delimiter string. The original string is split where the delimiter string occurs, and the delimiter itself is discarded. Multiple consecutive delimiters are not treated the same as a single occurrence, but rather cause empty strings to be created.
>>> "This is a sentence.".split(' ')
['This', 'is', 'a', 'sentence.']
>>> "Earth,Stars,Sun,Moon".split(',')
['Earth', 'Stars', 'Sun', 'Moon']
>>> " This is a sentence. ".split(' ')
['', 'This', 'is', '', '', '', 'a', 'sentence.', '', '']
>>> "This is a sentence.".split('e')
['This is a s', 'nt', 'nc', '.']
>>> "This is a sentence.".split('en')
['This is a s', 't', 'ce.']
The default is to split on every occurrence of the delimiter, however the maxsplit parameter limits the number of splittings that occur. The default value of -1 means no limit:
>>> "This is a sentence.".split('e', maxsplit=0)
['This is a sentence.']
>>> "This is a sentence.".split('e', maxsplit=1)
['This is a s', 'ntence.']
>>> "This is a sentence.".split('e', maxsplit=2)
['This is a s', 'nt', 'nce.']
>>> "This is a sentence.".split('e', maxsplit=-1)
['This is a s', 'nt', 'nc', '.']
str.rsplit(sep=None, maxsplit=-1)
str.rsplit ("right split") differs from str.split ("left split") when maxsplit is specified. The splitting starts at the end of the string rather than at the beginning:
>>> "This is a sentence.".rsplit('e', maxsplit=1)
['This is a sentenc', '.']
>>> "This is a sentence.".rsplit('e', maxsplit=2)
['This is a sent', 'nc', '.']
Note: Python specifies the maximum number of splits performed, while most other programming languages specify the maximum number of substrings created. This may create confusion when porting or comparing code.
Replace all occurrences of one substring with another substring:
Python's str type also has a method for replacing occurrences 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 replaced by the new substring. The optional argument count specifies the number of replacements to be made:
For example, in order to replace 'foo' with 'spam' in the following string, we can call str.replace with old = 'foo' and new = 'spam':
>>> "Make sure to foo your sentence.".replace('foo', 'spam')
"Make sure to spam your sentence."
Testing what a string is composed of:
str.isalpha:
str.isalpha takes no arguments and returns True if the all characters in a given string are alphabetic,
for example:
>>> "Hello World".isalpha() # contains a space
False
>>> "Hello2World".isalpha() # contains a number
False
str.isupper, str.islower, str.istitle:
These methods test the capitalization in a given string.
str.isupper is a method that returns True if all characters in a given string are uppercase and False otherwise.
>>> "HeLLO WORLD".isupper()
False
>>> "HELLO WORLD".isupper()
True
>>> "".isupper()
False
Conversely, str.islower is a method that returns True if all characters in a given string are lowercase and False otherwise.
>>> "Hello world".islower()
False
>>> "hello world".islower()
True
>>> "".islower()
False
str.istitle returns True if the given string is title cased; that is, every word begins with an uppercase character followed by lowercase characters.
>>> "hello world".istitle()
False
>>> "Hello world".istitle()
False
>>> "Hello World".istitle()
True
>>> "".istitle()
False
str.isdecimal, str.isdigit, str.isnumeric:
str.isdecimal returns whether the string is a sequence of decimal digits, suitable for representing a decimal number.
str.isdigit includes digits not in a form suitable for representing a decimal number, such as superscript digits.
str.isnumeric includes any number values, even if not digits, such as values outside the range 0-9.
isdecimal isdigit isnumeric
Bytestrings (bytes in Python 3, str in Python 2), only support isdigit, which only checks for basic ASCII digits.
As with str.isalpha, the empty string evaluates to False.
str.isalnum
This is a combination of str.isalpha and str.isnumeric, specifically it evaluates to True if all characters in the given string are alphanumeric, that is, they consist of alphabetic or numeric characters:
>>> "Hello2World".isalnum()
True
>>> "HelloWorld".isalnum()
True
>>> "2016".isalnum()
True
>>> "Hello World".isalnum() # contains whitespace
False
str.isspace
Evaluates to True if the string contains only whitespace characters.
>>> "\t\r\n".isspace()
True
>>> " ".isspace()
True
Sometimes a string looks “empty” but we don't know whether it's because it contains just whitespace or no character at all
>>> "".isspace()
False
String Contains:
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
Join a list of strings into one string:
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","time"])
"once upon a time"
The following example separates the string elements with three hyphens.
>>> "---".join(["once", "upon", "a", "time"])
"once---upon---a---time"
Counting number of times a substring appears in a string:
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 indicate the beginning and the end in which the search will take
place. By default start = 0 and end = len(str) meaning the whole string will be searched:
>>> s = "She sells seashells by the seashore."
>>> s.count("sh")
2
>>> s.count("se")
3
>>> s.count("sea")
2
>>> s.count("seashells")
1
Justify strings:
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 interstates_lengths.items():
miles,kms = length
print('{} -> {} mi. ({} km.)'.format(str(road).rjust(4), str(miles).ljust(4),
str(kms).ljust(4)))
40 -> 2555 mi. (4112 km.)
19 -> 63 mi. (102 km.)
5 -> 1381 mi. (2222 km.)
93 -> 189 mi. (305 km.)
ljust and rjust are very similar. Both have a width parameter and an optional fillchar parameter. Any string created by these functions is at least as long as the width parameter that was passed into the function. If the string is longer than width alread, it is not truncated. The fillchar argument, which defaults to the space character ' ' must be a single character, not a multicharacter string.
Test the starting and ending characters of a string:
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 its name implies, str.startswith is used to test whether a given string starts with the given characters in prefix.
>>> s = "This is a test string"
>>> s.startswith("T")
True
>>> s.startswith("Thi")
True
>>> s.startswith("thi")
False
The optional arguments start and end specify the start and end points from which the testing will start and finish.
In the following example, by specifying a start value of 2 our string will be searched from position 2 and afterwards:
>>> s.startswith("is", 2)
True
This yields True since s[2] == 'i' and s[3] == 's'.
You can also use a tuple to check if it starts with any of a set of strings
>>> s.startswith(('This', 'That'))
True
>>> s.startswith(('ab', 'bc'))
False
str.endswith(prefix[, start[, end]])
str.endswith is exactly similar to str.startswith with the only difference being that it searches for ending
characters and not starting characters. For example, to test if a string ends in a full stop, one could write:
>>> s = "this ends in a full stop."
>>> s.endswith('.')
True
>>> s.endswith('!')
False
as with startswith more than one characters can used as the ending sequence:
>>> s.endswith('stop.')
True
>>> s.endswith('Stop.')
False
You can also use a tuple to check if it ends with any of a set of strings
>>> s.endswith(('.', 'something'))
True
>>> s.endswith(('ab', 'bc'))
False
Comments
Post a Comment