Text or String is one of the most common data type that we have to work as a software developer. In this tutorial, we’re gonna look at almost Python String methods which are important when working with text.
Create raw String with escape characters
We use r
before quotation mark of a string:
>>> print(r'Play with \'escape\' characters\n\t\\\\')
Play with \'escape\' characters\n\t\\\\
Create multi-lines String
When we put string inside three single quotes/ three double quotes, any quote, tab, or newline will be considered part of the string.
For example:
>>> '''We solve problems with 2 kinds of approach:
...
... - 'grok' so thoroughly that the observer becomes a part of the observed.
... - contrasts 'zen', which is a similar supernatural understanding experienced as a single brief flash.
...
... That's the idea our logo is built around.'''
"We solve problems with 2 kinds of approach:\n\n- 'grok' so thoroughly that the observer becomes a part of the observed.\n- contrasts 'zen', which is a similar supernatural understanding experienced as a single brief flash.\n\nThat's the idea our logo is built around."
Or we can test using a .py
file with the code below and run it:
print('''We solve problems with 2 kinds of approach:
- 'grok' so thoroughly that the observer becomes a part of the observed.
- contrasts 'zen', which is a similar supernatural understanding experienced as a single brief flash.
That's the idea our logo is built around.''')
Output:
We solve problems with 2 kinds of approach:
- 'grok' so thoroughly that the observer becomes a part of the observed.
- contrasts 'zen', which is a similar supernatural understanding experienced as a single brief flash.
That's the idea our logo is built around.
Get characters by Index and Slide String
We can apply indexes and slides on Python String with the same way as Python List. Just think of a string as a list and each character in the string as an item:
>>> site = 'ozenero'
>>> site[0]
'g'
>>> site[1]
'r'
>>> site[0:4]
'grok'
>>> site[4:]
'onez'
Python Transform String methods
Upper and Lower String
upper()
and lower()
methods return a new string (not change the original string) with all letters have been converted to uppercase or lowercase:
>>> site = 'ozenero'
>>> site.upper()
'GROKONEZ'
>>> site
'ozenero'
>>> site = site.upper()
>>> site
'GROKONEZ'
>>> site.lower()
'ozenero'
Swap Upper-Lower String
swapcase()
returns a new string that converts all uppercase characters to lowercase and all lowercase characters to uppercase characters.
>>> 'ozenero technology'.swapcase() 'GROKONEZ TECHNOLOGY' >>> 'GROKONEZ technology'.swapcase() 'ozenero TECHNOLOGY'
Capitalize first letter
- capitalize()
converts first letter of a string to uppercase letter and lowercases others:
>>> 'ozenero programming tutorials'.capitalize()
'Grokonez programming tutorials'
>>> 'GROKONEZ TUTORIALS'.capitalize()
'Grokonez tutorials'
- title()
returns a string with first letter of each word capitalized:
>>> 'ozenero programming tutorials'.title()
'Grokonez Programming Tutorials'
>>> 'GROKONEZ programming tutorials'.title()
'Grokonez Programming Tutorials'
Python Validate String methods
Uppercase & Lowercase
isupper()
and islower()
methods return:
- True
if:
+ the string has at least one letter (not number)
+ all the letters are uppercase/lowercase
- False
for other cases
For example:
>>> site = 'Grokonez'
>>> site.isupper()
False
>>> site.islower()
False
>>> site = 'ozenero'
>>> site.islower()
True
>>> numberString = '12345'
>>> numberString.isupper()
False
>>> numberString.islower()
False
>>> flex = 'abc123'
>>> flex.islower()
True
>>> flex = 'ABC123'
>>> flex.isupper()
True
Letters & Numbers
- isalpha()
returns True
if the string has only letters (not blank).
>>> 'ozenero'.isalpha()
True
>>> 'jsa2018'.isalpha()
False
>>> 'jsa-tech'.isalpha()
False
>>> 'jsa Technology'.isalpha()
False
- isalnum()
returns True
if the string has only letters and numbers (not blank).
>>> 'ozenero2018'.isalnum()
True
>>> 'jsa-2018'.isalnum()
False
>>> 'jsa Technology'.isalnum()
False
- isdecimal()
returns True
if the string has only numeric characters (not blank).
>>> '2019'.isdecimal()
True
>>> '2016and2017'.isdecimal()
False
>>> '2017-2018'.isdecimal()
False
Space
isspace()
returns True
if the string has only spaces, tabs, and newlines (not blank).
>>> '\n \t'.isspace()
True
>>> '1 '.isspace()
False
Title type
istitle()
returns True
if the string has only words that begin with an uppercase letter followed by only lowercase letters.
>>> 'Grokonez Technology'.istitle()
True
>>> 'JSA Technology'.istitle()
False
>>> 'Jsa technology'.istitle()
False
Start/End with
- startswith()
returns True
if the string value begins with the string passed to the method:
>>> 'ozenero Technology'.startswith('ozenero')
True
>>> 'ozenero Technology'.startswith('Tech')
False
>>> 'ozenero Technology'.startswith('grokee')
False
>>> 'ozenero Technology'.startswith('ozenero Technology')
True
- endswith()
returns True
if the string value ends with the string passed to the method:
>>> 'ozenero Technology'.endswith('logy')
True
>>> 'ozenero Technology'.endswith('biology')
False
>>> 'ozenero Technology'.endswith('ozenero Techno')
False
>>> 'ozenero Technology'.endswith('ozenero Technology')
True
Python Join and Split String methods
Join strings
We use join()
to join a list of string into a single string value. The returned string is the concatenation of each string in the passed-in list.
>>> ', '.join(['dog','cat','tiger'])
'dog, cat, tiger'
>>> ' '.join(['ozenero','Programming','tutorials'])
'ozenero Programming tutorials'
Split string
split()
is called on a string value and returns a list of strings.
>>> 'My website is ozenero.com'.split()
['My', 'website', 'is', 'ozenero.com']
By default, the string is split wherever a whitespace characters (space, tab, or newline) is found. We can pass a delimiter string to the split()
method to specify the string to split upon.
>>> 'My website is ozenero.com'.split('e')
['My w', 'bsit', ' is grokon', 'z.com']
Partition string
partition()
gets argument string as a separator, then:
- splits the string at the first occurrence of separator
- returns a tuple containing 3 parts: the string before separator, separator, and the part after separator.
>>> 'ozenero programming tutorials'.partition(' ')
('ozenero', ' ', 'programming tutorials')
>>> 'ozenero programming tutorials'.partition('ming ')
('ozenero program', 'ming ', 'tutorials')
Python Justify Text
Right Justified
rjust()
returns a right-justified string with given minimum width.
>>> 'ozenero'.rjust(10)
' ozenero'
# 10 = 2 spaces + 8 letters
>>> 'ozenero'.rjust(5)
'ozenero'
By default, spaces will be filled on the left. We can specify a fill character with second argument:
>>> 'ozenero'.rjust(10,'=')
'==ozenero'
Left Justified
ljust()
returns a left-justified string with given minimum width.
>>> 'ozenero'.ljust(10)
'ozenero '
# 10 = 8 letters + 2 spaces
>>> 'ozenero'.ljust(5)
'ozenero'
By default, spaces will be filled on the right. We can specify a fill character with second argument:
>>> 'ozenero'.ljust(10,'=')
'ozenero=='
Center
center()
works like ljust()
and rjust()
, but centers the text rather than justifying it to the left or right.
>>> 'ozenero'.center(10)
' ozenero '
>>> 'ozenero'.center(5)
'ozenero'
>>> 'ozenero'.center(10,'=')
'=ozenero='
Strip off characters
- strip()
returns a new string without any whitespace characters at the beginning or end:
>>> ' ozenero '.strip()
'ozenero'
- lstrip()
removes whitespace characters from the left, and rstrip()
removes whitespace characters from the right ends:
>>> ' ozenero '.lstrip()
'ozenero '
>>> ' ozenero '.rstrip()
' ozenero'
We can pass a string argument to this method to specify the set of characters to be removed:
>>> 'TutsTutsozeneroTutsPythonTuts'.strip('sTut')
'ozeneroTutsPython'
>>> 'TutsTutsozeneroTutsPythonTuts'.lstrip('sTut')
'ozeneroTutsPythonTuts'
>>> 'TutsTutsozeneroTutsPythonTuts'.rstrip('sTut')
'TutsTutsozeneroTutsPython'
*Note: The order of the characters in the string passed to strip()
does not matter: strip('sTut')
will do the same thing as strip('tTus')
or strip('Tuts')
.
Check Substring in String
We can know whether a string is or is not in a parent string using in
or not in
operators:
>>> 'grok' in 'ozenero Technology'
True
>>> 'konez' in 'ozenero Technology'
True
>>> 'Grok' in 'ozenero Technology'
False
>>> '' in 'ozenero Technology'
True
>>> 'gray' not in 'ozenero Technology'
True
>>> 'konez' not in 'ozenero Technology'
False
Python String Finding methods
Count Substrings
count()
returns the number of substrings inside the given string.
>>> string = 'ozenero is derived from the words grok and konez'
>>> substring = 'grok'
>>> string.count(substring)
2
Find index of Substring
index() & rindex()
- index()
returns the index of first occurrence of the substring (if found) in the string, or throw an exception when the substring is not found.
>>> string = 'ozenero is derived from the words grok and konez'
>>> substring = 'konez'
>>> string.index(substring)
3
# search within string[5:]
>>> string.index(substring, 5)
44
# search in string[2:10]='okonez i'
>>> string.index(substring, 2, 10)
3
# search within string[2:7]='okone'
>>> string.index(substring, 2, 7)
Traceback (most recent call last):
File "", line 1, in
ValueError: substring not found
- rindex()
returns the index of last occurrence of the substring (if found) in the string, or throw an exception when the substring is not found.
>>> string = 'ozenero is derived from the words grok and konez'
>>> substring = 'konez'
>>> string.rindex(substring)
44
# search in string[:42]='ozenero is derived from the words grok an'
>>> string.rindex(substring, 0, 42)
3
# search in string[10:42]='s derived from the words grok an'
>>> string.rindex(substring, 10, 42)
Traceback (most recent call last):
File "", line 1, in
ValueError: substring not found
find() & rfind()
- Similar to index()
/rindex()
method, but there is only one difference: find()
/rfind()
return -1
if not found.
>>> string = 'ozenero is derived from the words grok and konez'
>>> substring = 'konez'
>>> string.find(substring)
3
# search within string[5:]
>>> string.find(substring, 5)
44
# search in string[2:10]='okonez i'
>>> string.find(substring, 2, 10)
3
# search within string[2:7]='okone'
>>> string.find(substring, 2, 7)
-1
# === rfind() ===
>>> string.rfind(substring)
44
# search in string[:42]='ozenero is derived from the words grok an'
>>> string.rfind(substring, 0, 42)
3
# search in string[10:42]='s derived from the words grok an'
>>> string.rfind(substring, 10, 42)
-1
Find and Replace
replace()
returns the string in which, every substring is replaced with argument string.
>>> string = 'ozenero is derived from the words grok and konez'
>>> string.replace('nez', 'zen')
'grokozen is derived from the words grok and kozen'