Python String Function
Python String Function
Python has a set of built-in methods that you can use on strings.
Note: All string methods returns new values. They do not change the original string.
Method Description
endswith() Returns true if the string ends with the specified value
find() Searches the string for a specified value and returns the position of where
it was found
index() Searches the string for a specified value and returns the position of where
it was found
isalpha() Returns True if all characters in the string are in the alphabet
isascii() Returns True if all characters in the string are ascii characters
islower() Returns True if all characters in the string are lower case
isupper() Returns True if all characters in the string are upper case
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
rpartition() Returns a tuple where the string is parted into three parts
rsplit() Splits the string at the specified separator, and returns a list
split() Splits the string at the specified separator, and returns a list
startswith() Returns true if the string starts with the specified value
swapcase() Swaps cases, lower case becomes upper case and vice versa
title() Converts the first character of each word to upper case
zfill() Fills the string with a specified number of 0 values at the beginning
Syntax
string.capitalize()
Parameter Values
No parameters
More Examples
Example
The first character is converted to upper case, and the rest are converted to lower case:
txt = "python is FUN!"
x = txt.capitalize()
print (x)
Output:
Python is fun!
Example
See what happens if the first character is a number:
txt = "36 is my age."
x = txt.capitalize()
print (x)
Output:
36 is my age
Python String casefold() Method
Syntax
string.casefold()
Parameter Values
No parameters
Print the word "banana", taking up the space of 20 characters, with "banana" in the middle:
txt = "banana"
x = txt.center(20)
print(x)
Output:
banana
The center() method will center align the string, using a specified character (space is default)
as the fill character.
Syntax
string.center(length, character)
Parameter Values
Parameter Description
length Required. The length of the returned string
character Optional. The character to fill the missing space on each side. Default is
" " (space)
Example
txt = "banana"
x = txt.center(20, "O")
print(x)
Output:
OOOOOOObananaOOOOOOO
Return the number of times the value "apple" appears in the string:
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple")
print(x)
Output:
2
Definition and Usage
The count() method returns the number of times a specified value appears in the string.
Syntax
string.count(value, start, end)
Parameter Values
Parameter Description
end Optional. An Integer. The position to end the search. Default is the end
of the string
Example
Search from position 10 to 24:
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple", 10, 24)
print(x)
Output:
1
Output:
b'My name is St\xc3\xe5le'
The encode() method encodes the string, using the specified encoding. If no encoding is
specified, UTF-8 will be used.
Syntax
string.encode(encoding=encoding, errors=errors)
Output:
True
The endswith() method returns True if the string ends with the specified value, otherwise
False.
Syntax
Parameter Description
Example
Output:
True
Example
Output:
False
Syntax
string.expandtabs(tabsize)
Parameter Values
Parameter Description
Example
See the result using different tab sizes:
txt = "H\te\tl\tl\to"
print(txt)
print(txt.expandtabs())
print(txt.expandtabs(2))
print(txt.expandtabs(4))
print(txt.expandtabs(10))
Output:
H e l l o
H e l l o
Hello
H e l l o
H e l l o
Syntax
string.find(value, start, end)
Parameter Values
Parameter Description
Example
Where in the text is the first occurrence of the letter "e"?:
txt = "Hello, welcome to my world."
x = txt.find("e")
print(x)
Output:
1
Example
Where in the text is the first occurrence of the letter "e" when you only search between
position 5 and 10?:
txt = "Hello, welcome to my world."
x = txt.find("e", 5, 10)
print(x)
Output:
8
Example
If the value is not found, the find() method returns -1, but the index() method will raise an
exception:
txt = "Hello, welcome to my world."
print(txt.find("q"))
print(txt.index("q"))
Output:
-1
Traceback (most recent call last):
File "demo_ref_string_find_vs_index.py", line 4 in <module>
print(txt.index("q"))
ValueError: substring not found
value1, Required. One or more values that should be formatted and inserted in
value2... the string.
The Placeholders
The placeholders can be identified using named indexes {price}, numbered indexes {0}, or
even empty placeholders {}.
Example
Using different placeholder values:
txt1 = "My name is {fname}, I'm {age}".format(fname = "John", age = 36)
txt2 = "My name is {0}, I'm {1}".format("John",36)
txt3 = "My name is {}, I'm {}".format("John",36)
Output:
My name is John, I'm 36
My name is John, I'm 36
My name is John, I'm 36
Syntax
string.index(value, start, end)
Parameter Values
Parameter Description
end Optional. Where to end the search. Default is to the end of the string
Example
Where in the text is the first occurrence of the letter "e"?:
txt = "Hello, welcome to my world."
x = txt.index("e")
print(x)
Output:
1
Example
Where in the text is the first occurrence of the letter "e" when you only search between
position 5 and 10?:
txt = "Hello, welcome to my world."
x = txt.index("e", 5, 10)
print(x)
Output:
8
Example
If the value is not found, the find() method returns -1, but the index() method will raise an
exception:
txt = "Hello, welcome to my world."
print(txt.find("q"))
print(txt.index("q"))
Output:
-1
Traceback (most recent call last):
File "demo_ref_string_find_vs_index.py", line 4 in <module>
print(txt.index("q"))
ValueError: substring not found
Syntax
string.isalnum()
Parameter Values
No parameters.
Example
Check if all the characters in the text is alphanumeric:
txt = "Company 12"
x = txt.isalnum()
print(x)
Output:
False
Syntax
string.isalpha()
Parameter Values
No parameters.
Example
Check if all the characters in the text is alphabetic:
txt = "Company10"
x = txt.isalpha()
print(x)
Output:
False
Syntax
string.isdecimal()
Parameter Values
No parameters.
Syntax
string.isdigit()
Parameter Values
No parameters.
Syntax
string.isidentifier()
Parameter Values
No parameters.
Example
Check if the strings are valid identifiers:
a = "MyFolder"
b = "Demo002"
c = "2bring"
d = "my demo"
print(a.isidentifier())
print(b.isidentifier())
print(c.isidentifier())
print(d.isidentifier())
Output:
True
True
False
False
Syntax
string.islower()
Parameter Values
No parameters.
Example
Check if all the characters in the texts are in lower case:
a = "Hello world!"
b = "hello 123"
c = "mynameisPeter"
print(a.islower())
print(b.islower())
print(c.islower())
Output:
False
True
False
Syntax
string.isnumeric()
Parameter Values
No parameters.
Syntax
string.isprintable()
Parameter Values
No parameters.
Example
Check if all the characters in the text are printable:
txt = "Hello!\nAre you #1?"
x = txt.isprintable()
print(x)
Output:
False
Syntax
string.isspace()
Parameter Values
No parameters.
Example
Check if all the characters in the text are whitespaces:
txt = " s "
x = txt.isspace()
print(x)
Output:
False
Syntax
string.istitle()
Parameter Values
No parameters.
Example
Check if each word start with an upper case letter:
a = "HELLO, AND WELCOME TO MY WORLD"
b = "Hello"
c = "22 Names"
d = "This Is %'!?"
print(a.istitle())
print(b.istitle())
print(c.istitle())
print(d.istitle())
Output:
False
True
True
True
Syntax
string.isupper()
Parameter Values
No parameters.
Example
Check if all the characters in the texts are in upper case:
a = "Hello World!"
b = "hello 123"
c = "MY NAME IS PETER"
print(a.isupper())
print(b.isupper())
print(c.isupper())
Output:
False
False
True
Syntax
string.join(iterable)
Parameter Values
Parameter Description
iterable Required. Any iterable object where all the returned values are strings
Example
Join all items in a dictionary into a string, using the word "TEST" as separator:
myDict = {"name": "John", "country": "Norway"}
mySeparator = "TEST"
x = mySeparator.join(myDict)
print(x)
Output:
nameTESTcountry
Note: In the result, there are actually 14 whitespaces to the right of the word banana.
Syntax
string.ljust(length, character)
Parameter Values
Parameter Description
character Optional. A character to fill the missing space (to the right of the string).
Default is " " (space).
Example
Using the letter "A" as the padding character:
txt = "banana"
x = txt.ljust(20, "A")
print(x)
Output:
bananaAAAAAAAAAAAAAA
Syntax
string.lower()
Parameter Values
No parameters
Syntax
string.lstrip(characters)
Parameter Values
Parameter Description
Example
Remove the leading characters:
txt = ",,,,,ssaaww.....banana"
x = txt.lstrip(",.asw")
print(x)
Output:
banana
Syntax
string.partition(value)
Parameter Values
Parameter Description
Example
If the specified value is not found, the partition() method returns a tuple containing: 1 - the
whole string, 2 - an empty string, 3 - an empty string:
txt = "I could eat bananas all day"
x = txt.partition("apples")
print(x)
Output:
('I could eat bananas all day', '', '')
Syntax
string.replace(oldvalue, newvalue, count)
Parameter Values
Parameter Description
count Optional. A number specifying how many occurrences of the old value
you want to replace. Default is all occurrences
Example
Replace all occurrence of the word "one":
txt = "one one was a race horse, two two was one too."
x = txt.replace("one", "three")
print(x)
Output:
three three was a race horse, two two was three too."
RegEx Module
Python has a built-in package called re, which can be used to work with Regular Expressions.
Import the re module:
import re
RegEx in Python
When you have imported the re module, you can start using regular expressions:
Search the string to see if it starts with "The" and ends with "Spain":
import re
txt = "The rain in Spain"
x = re.search("^The.*Spain$", txt)
Output:
YES! We have a match!
RegEx Functions
The re module offers a set of functions that allows us to search a string for a match:
Functio Description
n
split Returns a list where the string has been split at each match
| Either or "falls|stays"
Special Sequences
A special sequence is a \ followed by one of the characters in the list below, and has a special
meaning:
Characte Description Example
r
\W Returns a match where the string DOES NOT contain any "\W"
word characters
Sets
A set is a set of characters inside a pair of square brackets [] with a special meaning:
Set Description
[arn] Returns a match where one of the specified characters (a, r, or n) is present
[a-n] Returns a match for any lower case character, alphabetically between a and n
[0123] Returns a match where any of the specified digits (0, 1, 2, or 3) are present
[a-zA- Returns a match for any character alphabetically between a and z, lower case
Z] OR upper case
[+] In sets, +, *, ., |, (), $,{} has no special meaning, so [+] means: return a match
for any + character in the string
The list contains the matches in the order they are found.
If no matches are found, an empty list is returned:
Example
Return an empty list if no match was found:
import re
txt = "The rain in Spain"
x = re.findall("Portugal", txt)
print(x)
Output:
[]
No match
You can control the number of occurrences by specifying the maxsplit parameter:
Example
Split the string only at the first occurrence:
import re
txt = "The rain in Spain"
x = re.split("\s", txt, 1)
print(x)
Output:
['The', 'rain in Spain']
You can control the number of replacements by specifying the count parameter:
Example
Replace the first 2 occurrences:
import re
txt = "The rain in Spain"
x = re.sub("\s", "9", txt, 2)
print(x)
Output:
The9rain9in Spain
Match Object
A Match Object is an object containing information about the search and the result.
Note: If there is no match, the value None will be returned, instead of the Match Object.
Example
Do a search that will return a Match Object:
import re
txt = "The rain in Spain"
x = re.search("ai", txt)
print(x) #this will print an object
Output:
<_sre.SRE_Match object; span=(5, 7), match='ai'>
The Match object has properties and methods used to retrieve information about the search,
and the result:
.span() returns a tuple containing the start-, and end positions of the match.
.string returns the string passed into the function
.group() returns the part of the string where there was a match
Example
Print the position (start- and end-position) of the first match occurrence.
The regular expression looks for any words that starts with an upper case "S":
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.span())
Output:
(12, 17)
Example
Print the string passed into the function:
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.string)
Output:
The rain in Spain
Example
Print the part of the string where there was a match.
The regular expression looks for any words that starts with an upper case "S":
import re
Metacharacters
Metacharacters are characters with a special meaning:
[] A set of characters "[a-m]"
import re
txt = "The rain in Spain"
#Find all lower case characters alphabetically between "a" and "m":
x = re.findall("[a-m]", txt)
print(x)
Output:
['h', 'e', 'a', 'i', 'i', 'a', 'i']
\ Signals a special sequence (can also be used to escape special characters) "\d"
import re
txt = "That will be 59 dollars"
#Find all digit characters:
x = re.findall("\d", txt)
print(x)
Output:
['5', '9']
Output
['hello']
| Either or "falls|stays"
import re
txt = "The rain in Spain falls mainly in the plain!"
#Check if the string contains either "falls" or "stays":
x = re.findall("falls|stays", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
Output:
['falls']
Yes, there is at least one match!
Special Sequences
A special sequence is a \ followed by one of the characters in the list below, and has a special
meaning:
Characte Description Example
r
Output:
[]
No match
import re
txt = "The rain in Spain"
#Check if "ain" is present at the end of a WORD:
x = re.findall(r"ain\b", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
Output:
['ain', 'ain']
Yes, there is at least one match!
\ Returns a match where the specified characters are present, but NOT at the r"\
B beginning (or at the end) of a word Bain"
(the "r" in the beginning is making sure that the string is being treated as a
"raw string") r"ain\
B"
import re
txt = "The rain in Spain"
#Check if "ain" is present, but NOT at the beginning of a word:
x = re.findall(r"\Bain", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
Output:
['ain', 'ain']
Yes, there is at least one match!
\d Returns a match where the string contains digits (numbers from 0-9) "\d"
import re
txt = "The rain in Spain"
#Check if the string contains any digits (numbers from 0-9):
x = re.findall("\d", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
Output:
[]
No match
\D Returns a match where the string DOES NOT contain digits "\D"
import re
txt = "The rain in Spain"
#Return a match at every no-digit character:
x = re.findall("\D", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
Output:
['T', 'h', 'e', ' ', 'r', 'a', 'i', 'n', ' ', 'i', 'n', ' ', 'S', 'p', 'a', 'i', 'n']
Yes, there is at least one match!
\s Returns a match where the string contains a white space character "\s"
import re
txt = "The rain in Spain"
#Return a match at every white-space character:
x = re.findall("\s", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
Output:
[' ', ' ', ' ']
Yes, there is at least one match!
\S Returns a match where the string DOES NOT contain a white space character "\S"
import re
txt = "The rain in Spain"
#Return a match at every NON white-space character:
x = re.findall("\S", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
Output:
['T', 'h', 'e', 'r', 'a', 'i', 'n', 'i', 'n', 'S', 'p', 'a', 'i', 'n']
Yes, there is at least one match!
\ Returns a match where the string contains any word characters (characters from "\
w a to Z, digits from 0-9, and the underscore _ character) w"
import re
txt = "The rain in Spain"
#Return a match at every word character (characters from a to Z, digits from 0-9, and the
underscore _ character):
x = re.findall("\w", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
Output:
['T', 'h', 'e', 'r', 'a', 'i', 'n', 'i', 'n', 'S', 'p', 'a', 'i', 'n']
Yes, there is at least one match!
\W Returns a match where the string DOES NOT contain any word characters "\W"
import re
Output:
[' ', ' ', ' ']
Yes, there is at least one match!
\Z Returns a match if the specified characters are at the end of the string "Spain\Z"
import re
txt = "The rain in Spain"
#Check if the string ends with "Spain":
x = re.findall("Spain\Z", txt)
print(x)
if x:
print("Yes, there is a match!")
else:
print("No match")
Output:
['Spain']
Yes, there is a match!
Sets
A set is a set of characters inside a pair of square brackets [] with a special meaning:
[arn] Returns a match where one of the specified characters (a, r, or n) is present
import re
txt = "The rain in Spain"
#Check if the string has any a, r, or n characters:
x = re.findall("[arn]", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
Output:
['r', 'a', 'n', 'n', 'a', 'n']
Yes, there is at least one match!
[a-n] Returns a match for any lower case character, alphabetically between a and n
import re
txt = "The rain in Spain"
#Check if the string has any characters between a and n:
x = re.findall("[a-n]", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
Output:
['h', 'e', 'a', 'i', 'n', 'i', 'n', 'a', 'i', 'n']
Yes, there is at least one match!
Output:
['T', 'h', 'e', ' ', 'i', ' ', 'i', ' ', 'S', 'p', 'i']
Yes, there is at least one match!
[0123] Returns a match where any of the specified digits (0, 1, 2, or 3) are present
import re
txt = "The rain in Spain"
#Check if the string has any 0, 1, 2, or 3 digits:
x = re.findall("[0123]", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
Output:
[]
No match
Output:
['8', '1', '1', '4', '5']
Yes, there is at least one match!
[0-5][0-9] Returns a match for any two-digit numbers from 00 and 59
import re
txt = "8 times before 11:45 AM"
#Check if the string has any two-digit numbers, from 00 to 59:
x = re.findall("[0-5][0-9]", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
Output:
['11', '45']
Yes, there is at least one match!
[a-zA-Z] Returns a match for any character alphabetically between a and z, lower case
OR upper case
import re
txt = "8 times before 11:45 AM"
#Check if the string has any characters from a to z lower case, and A to Z upper case:
x = re.findall("[a-zA-Z]", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
Output:
['t', 'i', 'm', 'e', 's', 'b', 'e', 'f', 'o', 'r', 'e', 'A', 'M']
Yes, there is at least one match!
[+ In sets, +, *, ., |, (), $,{} has no special meaning, so [+] means: return a match for
] any + character in the string
import re
txt = "8 times before 11:45 AM"
#Check if the string has any + characters:
x = re.findall("[+]", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
Output:
[]
No match
The “re” module which comes with every python installation provides regular expression
support.
The re.search() method takes two arguments, a regular expression pattern and a string and
searches for that pattern within the string. If the pattern is found within the string, search()
returns a match object or None otherwise. So in a regular expression, given a string,
determine whether that string matches a given pattern, and, optionally, collect substrings that
contain relevant information. A regular expression can be used to answer questions like −
Matching patterns
Regular expressions are complicated mini-language. They rely on special characters to match
unknown strings, but let's start with literal characters, such as letters, numbers, and the space
character, which always match them. Let's see a basic example:
Output:
regex matches: King