Python Strings
Python Strings
"My name is Kevin" – This is a string
Always remember that in Python, single quotes are similar to double quotes. However, triple quotes are
also used, but for multi-line string.
str = 'My name is Kevin!'
String Literals
In Python Strings, literals are enclosed in single, double quotes, and even triple quotes, for example:
#!/usr/bin/python
str1 = 'My name is Kevin!'
print (str1)
str2 = "My name is Kevin!"
print (str2)
https://studyopedia.com/python3/python-strings/ 3/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
p ( )
#multi-line string
str3 = """My name is Kevin and I
live in New York"""
print (str3)
My name is Kevin!
My name is Kevin!
My name is Kevin and I
live in New York
Above, under variable str3, you can see the string surrounded by three quotes. This forms the string,
multi-line.
myVar = 'My name is Kevin!'
print "Here's my name: ", myVar[11:16]
'
https://studyopedia.com/python3/python-strings/ i 4/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
Here's my name: Kevin
str = "This is Demo Text"
print("String = ",str)
# Negative Indexing
print("Accessing substring with negative indexing...")
print(str[-4:-2])
print(str[-6:-2])
print(str[-7:-5])
String = This is Demo Text
Accessing substring with negative indexing...
Te
o Te
mo
myVar = 'My name is Kevin!'
print "Character is: ", myVar[9]
Character is: s
https://studyopedia.com/python3/python-strings/ 6/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
## Escape Characters
# Form Feed
str = "This is \fDemo Text"
print("String = ",str)
# Octal Notation
str = '\110'
print("Octal Notation = ",str)
# Hexadecimal Notation
str = '\x57'
print("Hexadecimal Notation = ",str)
# Tab Notation
str = 'Demo\tText'
print("Hexadecimal Notation = ",str)
String = This is Demo Text
Octal Notation = H
Hexadecimal Notation = W
Hexadecimal Notation = Demo Text
## Concatenate Strings
# String1
str1 = "Money"
print("String 1 = ",str1)
# String2
str2 = "Heist"
print("String 2 = ",str2)
# concat
res = str1 + str2
print("Complete String = ",res)
String 1 = Money
String 2 = Heist
Complete String = MoneyHeist
String Operators
Python Strings comes with special Operators. Below are these operators, let’s say, we have two variables,
with values:
X = Study
Y = Now
https://studyopedia.com/python3/python-strings/ 8/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
# capitalize method
str = "captain philips"
print("Initial string = ",str);
res = str.capitalize()
print("Updated string = ",res);
Initial string = captain philips
Updated string = Captain philips
# capitalize method
str = "rON wEASLEY"
print("Initial string = ",str);
res = str.capitalize()
print("Updated string = ",res);
Initial string = rON wEASLEY
Updated string = Ron Weasley
https://studyopedia.com/python3/python-strings/ 10/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
# casefold method
str = "CAPTAIN PHILIPS"
print("Initial string = ",str);
res = str.casefold()
print("Updated string = ",res);
Initial string = CAPTAIN PHILIPS
Updated string = captain philips
# casefold method
str = "Gilderoy Lockhart"
print("Initial string = ",str);
res = str.casefold()
print("Updated string = ",res);
Initial string = Gilderoy Lockhart
Updated string = gilderoy lockhart
https://studyopedia.com/python3/python-strings/ 11/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
string.center(len, char)
Above, len is the length of the string, whereas, char is the character to filled on left and right side.
# center method
str = "DEMO"
print("Initial string = ",str);
res = str.center(10,"#")
print("Updated string = ",res);
Initial string = DEMO
Updated string = ###DEMO###
# center method
str = "TEST0.1"
print("Initial string = ",str);
https://studyopedia.com/python3/python-strings/ 12/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
print( Initial string = ,str);
res = str.center(10,"$")
print("Updated string = ",res);
Initial string = TEST0.1
Updated string = $TEST0.1$$
string.count(str, begn, end)
Above, str is the string, begn is the position to start the search and end is the position to end.
# count method
str = "This is one."
print("String = ",str);
res = str.count("This",0, 15)
print("Count of specific value = ",res);
https://studyopedia.com/python3/python-strings/ 13/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
String = This is one.
Count of specific value = 1
# count method
str = "This is demo. This is another demo."
print("String = ",str);
res = str.count("This")
print("Count of specific value = ",res);
String = This is demo. This is another demo.
Count of specific value = 2
# encode method
str = "This is årea."
print("String = ",str);
res = str.encode()
print("Encoded String = ",res);
https://studyopedia.com/python3/python-strings/ 14/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
String = This is årea.
Encoded String = b'This is \xc3\xa5rea.'
string.find(str, begin, end)
Above, str is the string to be searched for, begin is where to start the search and end is where the search
ends. Let us see an example:
# find method
str = "This is first text."
print("String = ",str);
res = str.find("text")
print("The string has a specific value at position = ",res);
String = This is first text.
The string has a specific value at position = 14
# find method
https://studyopedia.com/python3/python-strings/ 15/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
# find method
str = "This is first text. This is second text"
print("String = ",str);
res = str.find("text", 20, 40)
print("The string has a specific value at position = ",res);
String = This is first text. This is second text
The string has a specific value at position = 35
string.endswith(str, begin, end)
Above, str is the value to be checked if the string ends with, begin is the position where the search start,
whereas end is where the search ends.
# endswith method
str = "This is demo text. This is demo text2"
print("String = ",str);
res = str.endswith("text2")
print("The string ends with a specific value = ",res);
https://studyopedia.com/python3/python-strings/ 16/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
String = This is demo text. This is demo text2
The string ends with a specific value = True
# endswith method
str = "This is demo text. This is demo text2"
print("String = ",str);
res = str.endswith("text2", 7, 15)
print("The string ends with a specific value from position 7 to 15 = ",res);
String = This is demo text. This is demo text2
The string ends with a specific value from position 7 to 15 = False
Let us see an example, wherein we will set the tab size as the parameter if the expandtabs() method:
# expandtabs method
str = "D\te\tm\to\t"
https://studyopedia.com/python3/python-strings/ 17/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
st \te\t \to\t
print("Initial string (with default tab size 8) = ",str.expandtabs());
print("String (with tab size 2) = ",str.expandtabs(2));
print("String (with tab size 4) = ",str.expandtabs(4));
Initial string (with default tab size (8) = D e m o
String (with tab size 2) = D e m o
String (with tab size 4) = D e m o
# expandtabs method
str = "Test\tTest2"
print("String (with tab size 10) = ",str.expandtabs(10));
print("String (with tab size 0) = ",str.expandtabs(0));
String (with tab size 10) = Test Test2
String (with tab size 0) = TestTest2
string.index(str, begin, end)
https://studyopedia.com/python3/python-strings/ 18/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
Above, str is the value to be searched, begin is where the search begins and end is where it ends.
# index method
str = "This is first text."
print("String = ",str);
res = str.index("i")
print("The first occurrence of the specific value found at index = ",res);
String = This is first text.
The first occurrence of the specific value found at index = 2
Let us see another example of index() method with search beginning from a specific index:
# index method
str = "This is first text."
print("String = ",str);
res = str.index("i", 3, 10)
print("The first occurrence of the specific value found at index beginning from index 3 = ",res
String = This is first text.
The first occurrence of the specific value found at index beginning from index 3 = 5
https://studyopedia.com/python3/python-strings/ 19/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
# isalnum method
str = "JamesBond007"
print("String = ",str);
res = str.isalnum()
print("Are all the characters in the string alphanumeric = ",res);
String = JamesBond007
Are all the characters in the string alphanumeric = True
# isalnum method
str = "$$$$###55KP"
print("String = ",str);
res = str.isalnum()
print("Are all the characters in the string alphanumeric = ",res);
String = $$$$###55KP
Are all the characters in the string alphanumeric = False
# isalpha method
str1 = "JamesBond007"
print("String1 = ",str1);
res = str1.isalnum()
print("Are all the characters in String1 alphanumeric = ",res);
str2 = "TomCruise"
print("String2 = ",str2);
res = str2.isalpha()
print("Are all the characters in String2 alphabets = ",res);
String1 = JamesBond007
Are all the characters in String1 alphanumeric = True
String2 = TomCruise
Are all the characters in String2 alphabets = True
# isalpha method
str1 = "@@Jack"
print("String1 = ",str1);
res = str1.isalnum()
print("Are all the characters in String1 alphanumeric = ",res);
str2 = "$$$$$K"
print("String2 = ",str2);
res = str2.isalpha()
print("Are all the characters in String2 alphabets = ",res);
String1 = @@Jack
Are all the characters in String1 alphanumeric = False
String2 = $$$$$K
Are all the characters in String2 alphabets = False
# isdecimal method
str = "u0042"
print("String = ",str);
res = str.isalnum()
https://studyopedia.com/python3/python-strings/ 22/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
()
print("Are all the characters in String (Unicode) are decimal = ",res);
String = u0042
Are all the characters in String (Unicode) are decimal = True
# isdigit method
str = "47890"
print("String = ",str);
res = str.isdigit()
print("Are all the characters in String are digits = ",res);
String = 47890
Are all the characters in String are digits = True
# isdigit method
https://studyopedia.com/python3/python-strings/ 23/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
sd g t et od
str = "demo666"
print("String = ",str);
res = str.isdigit()
print("Are all the characters in String are digits = ",res);
String = demo666
Are all the characters in String are digits = False
Note: Valid identifier doesn’t begin with a number. It doesn’t even include spaces. However, a string is a
valid identifier, if it includes (a-z), (0-9), or (_).
# isidentifier method
str = "demo666"
print("String = ",str);
res = str.isidentifier()
print("Is the string, a valid identifier = ",res);
String = demo666
https://studyopedia.com/python3/python-strings/ 24/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
String = demo666
Is the string, a valid identifier = True
# isidentifier method
str = "38768"
print("String = ",str);
res = str.isidentifier()
print("Is the string, a valid identifier = ",res);
String = 38768
Is the string, a valid identifier = False
# islower method
str = "jackryan"
print("String = ",str);
res = str.islower()
print("Are all the characters in the string lowercase = ",res);
https://studyopedia.com/python3/python-strings/ 25/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
String = jackryan
Are all the characters in the string lowercase = True
# islower method
str = "JackRyan"
print("String = ",str);
res = str.islower()
print("Are all the characters in the string lowercase = ",res);
String = JackRyan
Are all the characters in the string lowercase = False
# isnumeric method
str = "987987"
print("String = ",str);
https://studyopedia.com/python3/python-strings/ 26/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
p t( St g ,st );
res = str.isnumeric()
print("Are all the characters in the string numeric = ",res);
String = 987987
Are all the characters in the string numeric = True
# isnumeric method
str = "Sylvester"
print("String = ",str);
res = str.isnumeric()
print("Are all the characters in the string numeric = ",res);
String = Sylvester
Are all the characters in the string numeric = False
https://studyopedia.com/python3/python-strings/ 27/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
# isprintable method
str = "Sylvester"
print("String = ",str);
res = str.isprintable()
print("Are all the characters in the string printable = ",res);
String = Sylvester
Are all the characters in the string printable = True
# isprintable method
str = "Sylvester\tStallone"
print("String = ",str);
res = str.isprintable()
print("Are all the characters in the string printable = ",res);
String = Sylvester Stallone
Are all the characters in the string printable = False
# isspace method
str = " "
print("String = ",str);
res = str.isspace()
print("Are all the characters in the string whitespace = ",res);
String =
Are all the characters in the string whitespace = True
# isspace method
str = "One Two Three"
print("String = ",str);
res = str.isspace()
print("Are all the characters in the string whitespace = ",res);
String = One Two Three
Are all the characters in the string whitespace = False
The istitle() method in Python eturns TRUE, if all the string considers the rules of a title (i.e. uppercase
title), else FALSE is returned. Uppercase titles, example, “Demo Text”, “Mark”, etc.
# istitle method
str = "Mark"
print("String = ",str);
res = str.istitle()
print("Is the string considers the Uppercase title rule = ",res);
String = Mark
Is the string considers the Uppercase title rule = True
# istitle method
str = "demo text"
print("String = ",str);
res = str.istitle()
print("Is the string considers the Uppercase title rule = ",res);
String = demo text
I th t i
https://studyopedia.com/python3/python-strings/
id th U titl l F l 30/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
Is the string considers the Uppercase title rule = False
Python isupper()method
The isupper() method in Python returns TRUE, if all the characters in the string are uppercase, else
FALSE is returned.
# isupper
str = "MARK RUFFALO"
print("String = ",str);
res = str.isupper()
print("Are all the characters in the string uppercase =",res);
String = MARK RUFFALO
Are all the characters in the string uppercase = True
# isupper
str = "demo TEXT"
print("String = ",str);
res = str.isupper()
print("Are all the characters in the string uppercase =",res);
Th t t
https://studyopedia.com/python3/python-strings/ i f ll 31/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
The output is as follows:
String = demo TEXT
Are all the characters in the string uppercase = False
Let us see an example of the join() method, wherein we have Dictionary iterable, returning keys:
# join method
dict = {"id": "S01", "name": "Frank"}
print("Dictionary = ",dict)
# set separator
sep = "AAA"
res = sep.join(dict)
print("Dictionary iterable =", res)
Dictionary = {'name': 'Frank', 'id': 'S01'}
Dictionary iterable = nameAAAid
# join method
https://studyopedia.com/python3/python-strings/ 32/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
str = {"Frank", "Shaun", "Scarlett", "Morgan"}
print("String = ",str)
# set separator
sep = "$$"
res = sep.join(str)
print("String iterable =", res)
String = {'Scarlett', 'Shaun', 'Frank', 'Morgan'}
String iterable = Scarlett$$Shaun$$Frank$$Morgan
# lower method
str = "The Walking Dead"
print("String = ",str)
res = str.lower()
print("Updated String with all letters in lowercase =", res)
String = The Walking Dead
Updated String with all letters in lowercase = the walking dead
https://studyopedia.com/python3/python-strings/ 33/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
# lower method
str = "STRANGER THINGS"
print("String = ",str)
res = str.lower()
print("Updated String =", res)
String = STRANGER THINGS
Updated String = stranger things
lstrip(ch)
# lstrip method
str = "######STRANGER THINGS"
https://studyopedia.com/python3/python-strings/ 34/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
st S G GS
print("String = ",str)
res = str.lstrip("#")
print("Updated String =", res)
String = TRUE DETECTIVE
Updated String = TRUE DETECTIVE
replace(old, new, count)
Above, old is the value to be searched, new is the value to be replaced with old, and count is how many
occurrences of the old value to be replaced.
# replace method
str = "This is demo. This is another demo"
print("String = ",str)
res = str.replace("demo", "test", 2)
print("Updated String =", res)
String = This is demo. This is another demo
Updated String = This is test. This is another test
# replace method
str = "This is demo."
print("String = ",str)
res = str.replace("demo", "example")
print("Updated String =", res)
String = This is demo.
Updated String = This is example.
rfind(val, begin, end)
Above, val is the value to be searched, begin is from where the search begins, and end is where it ends.
# rfind method
str = "This is demo. This is another demo"
print("String = ",str)
res = str.rfind("demo")
print("Position of Last occurrence of a specific value =", res)
String = This is demo. This is another demo
Position of Last occurrence of a specific value = 30
# rfind method
str = "This is demo. This is another demo. Yet another demo."
print("String = ",str)
res = str.rfind("demo")
print("Position of Last occurrence a specific value =", res)
String = This is demo. This is another demo. Yet another demo.
Position of Last occurrence a specific value = 48
Python rindex()method
https://studyopedia.com/python3/python-strings/ 37/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
The rindex() method in Python is used to get the index of the last occurrence of a specific value. The
syntax is as follows:
rindex(val, begin, end)
Above, val is the value to be searched, begin is from where the search begins, and end is where it ends.
# rindex method
str = "This is test. This is another test"
print("String = ",str)
res = str.rindex("test")
print("Position of Last occurrence of a specific value =", res)
String = This is test. This is another test
Position of Last occurrence of a specific value = 30
# rindex method
str = "Test test test test test test test test"
print("String = " str)
https://studyopedia.com/python3/python-strings/ 38/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
print( String = ,str)
res = str.rindex("test")
print("Position of Last occurrence of a specific value =", res)
String = Test test test test test test test test
Position of Last occurrence of a specific value = 35
rjust(len, chars)
Above, len is the total length of the string to be returned, whereas chars are the characters to be files on
the left.
# rjust method
str = "American Vandal"
print("String = ",str)
res = str.rjust(25, "A")
print(res)
https://studyopedia.com/python3/python-strings/ 39/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
String = American Vandal
AAAAAAAAAAAmerican Vandal
# rjust method
str = "American"
print("String = ",str)
res = str.rjust(15, "A")
print(res, "Vandal")
String = American
AAAAAAAAAAAAAAAAAAmerican Vandal
rsplit(sep, split)
Above, sep is the separator used while splitting, and split is the number of splits to be performed. If you
won’t mention anything, that would mean “all occurrences”.
https://studyopedia.com/python3/python-strings/ 40/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
won t mention anything, that would mean all occurrences .
# rsplit method
str = "Football, Archery, Cricket, Squash, Hockey, Volleyball"
print("String = ",str)
# split returns a list with 2 elements
res = str.rsplit(", ", 2)
print(res)
String = Football, Archery, Cricket, Squash, Hockey, Volleyball
['Football, Archery, Cricket, Squash', 'Hockey', 'Volleyball']
Note: Above, since we mentioned 2 as a parameter for split, the output is two elements i.e.:
Element 1: 'Football, Archery, Cricket, Squash'
Element 2: 'Hockey', 'Volleyball'
# rsplit method
str = "Football, Archery, Cricket, Squash, Hockey, Volleyball"
print("String = ",str)
# return a comma separated list
res = str.rsplit(", ")
print(res)
https://studyopedia.com/python3/python-strings/ 41/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
print(res)
String = Football, Archery, Cricket, Squash, Hockey, Volleyball
['Football', 'Archery', 'Cricket', 'Squash', 'Hockey', 'Volleyball']
rstrip(ch)
# rstrip method
str = "STRANGER THINGS#######"
print("String = ",str)
res = str.rstrip("#")
print("Updated String =", res)
String = STRANGER THINGS#######
Updated String = STRANGER THINGS
https://studyopedia.com/python3/python-strings/ 42/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
Updated String = STRANGER THINGS
Let us see another example to implement the rstrip() method and remove whitespace from the right:
# rstrip method
str = "TRUE DETECTIVE "
print("String = ",str)
res = str.rstrip()
print("Updated String =", res)
String = TRUE DETECTIVE
Updated String = TRUE DETECTIVE
split(sep, split)
Above, sep is the separator used while splitting, and split is the number of splits to be performed. If you
won’t mention anything, that would mean “all occurrences”.
https://studyopedia.com/python3/python-strings/ 43/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
# split method
str = "Money Heist", "13 Reasons Why", "Stranger Things"
print("String = ",str)
res = str.split(", ")
print("Result =", res)
String = ('Money Heist', '13 Reasons Why', 'Stranger Things')
Let us see another example of the split() method, wherein 2 elements are returned after split:
# split method
str = "One$$Two$$Three$$Four"
print("String = ",str)
res = str.split("$$",1)
print("Result =", res)
String = One$$Two$$Three$$Four
Result = ['One', 'Two$$Three$$Four']
Note: Above, since we mentioned 2 as a parameter for split, the output is two elements i.e.:
Element 1: 'One'
Element 2: 'Two$$Three$$Four'
https://studyopedia.com/python3/python-strings/ 44/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
string.plitlines(linebrk)
Above, linebrk is a boolean value to set whether to allow line breaks or not.
# splitlines method
str = "One\nTwo\nThree\nFour"
res = str.splitlines(True)
print("Result =", res)
Result = ['One\n', 'Two\n', 'Three\n', 'Four']
# splitlines method
str = "One\nTwo\nThree\nFour"
res = str splitlines(True)
https://studyopedia.com/python3/python-strings/ 45/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
res = str.splitlines(True)
print("Result =", res)
Result = ['One', 'Two', 'Three', 'Four']
# splitlines method
str = "One\nTwo\nThree\nFour"
res = str.splitlines(False)
print("Result =", res)
Result = ['One', 'Two', 'Three', 'Four']
string.startswith(val, begin, end)
Above, val is the value to be checked where the string begins with, begin is the position where the search
https://studyopedia.com/python3/python-strings/ 46/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
# startswith method
str = "This is demo. This is another demo."
print("String = ",str)
res = str.startswith("other", 15, 25)
print("Does any word begin with other =", res)
String = This is demo. This is another demo.
Does any word begin with other = False
# startswith method
str = "This is demo. This is another demo."
print("String = ",str)
res = str.startswith("an", 22, 30)
print("Does any word begin with the an =", res)
String = This is demo. This is another demo.
Does any word begin with the an = True
https://studyopedia.com/python3/python-strings/ 47/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
# swapcase method
str = "This is demo. This is another demo."
print("String = ",str)
res = str.swapcase()
print("Case Swapped =", res)
String = This is demo. This is another demo.
Case Swapped = tHIS IS DEMO. tHIS IS ANOTHER DEMO.
# swapcase method
str = "STRANGER THINGS"
print("String = ",str)
res = str.swapcase()
print("Case Swapped =", res)
https://studyopedia.com/python3/python-strings/ 48/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
String = STRANGER THINGS
Case Swapped = stranger things
# title method
str = "STRANGER THINGS"
print("String = ",str)
res = str.title()
print("Updated String =", res)
String = STRANGER THINGS
Updated Strings = Stranger Things
# title method
str = "demo text"
https://studyopedia.com/python3/python-strings/ 49/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
str = demo text
print("String = ",str)
res = str.title()
print("Updated String =", res)
String = demo text
Updated String = Demo Text
# upper method
str = "The Walking Dead"
print("String = ",str)
res = str.upper()
print("Updated String with all letters in uppercase now =", res)
String = The Walking Dead
Updated String with all letters in uppercase now = THE WALKING DEAD
# upper method
str = "demo text"
print("String = ",str)
res = str.upper()
print("Updated String with all letters in uppercase now =", res)
String = demo text
Updated String with all letters in uppercase now = DEMO TEXT
zfill(len)
Above, the parameter len is the total length of the string including the zero fill.
# zfill method
str = "Wow!"
print("String = ",str)
res = str.zfill(7)
print("Updated String with zero fill =", res)
https://studyopedia.com/python3/python-strings/ 51/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
String = Wow!
Updated String with zero fill = 000Wow!
# zfill method
str = "155.898"
print("String = ",str)
res = str.zfill(15)
print("Updated String with zero fill =", res)
String = 155.898
Updated String with zero fill = 00000000155.898
string.strip(ch)
Above, the parameter ch are the set of characters to be removed from the beginning and end of the string.
https://studyopedia.com/python3/python-strings/ 52/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
# strip method
str = " 10.50 "
print("String = ",str)
res = str.strip()
print("Updated String by removing leading and trailing whitespaces =", res)
String = 10.50
Updated String by removing leading and trailing whitespaces = 10.50
Let us see another example to implement the strip() method in Python and removing leading and trailing
characters:
# strip method
str = "$,...jack...$"
print("String = ",str)
res = str.strip("$,.")
print("Updated String by removing leading and trailing characters =", res)
String = $,...jack...$
Updated String by removing leading and trailing characters = jack
https://studyopedia.com/python3/python-strings/ 53/56
3/5/22, 10:56 AM Python Strings with Examples - Studyopedia
In this guide, we saw how to work with Python Strings, accessing a specific character, escape characters
and the built-in string methods.
Read More
Functions in Python
Scope of Variables
Type Conversion
Python Lists
Python DateTime
Tuples in Python
Python Dictionary
} W
Share Print page 0 Likes
https://studyopedia.com/python3/python-strings/ 54/56