8/11/25, 1:36 PM Python String Functions With Examples
Home Blogs Data Science Business & Management Programming Online Trainings OFFER Placement Guarantee C
Home » Programming » Python » Python String Functions With Examples
Python String Functions With Examples
Gaurav Aggarwal Share
Python string is the sequence of Unicode characters represented in quotation marks. Single and double to triple quotes can be used to define a string
function. With Python programming language providing in-built functions and operators for performing operations in the string, handling it becomes a simpler
and easier task. The Python string is now immutable, meaning all in-built Python functions return a new string without modifying the original string. Today, we
will talk about various Python string functions, also known as string methods.
Table of Contents
Python String Functions With Examples
Python is one of the most famous programming languages in the world right now. It has a wide scope as it is a growing field. Understanding the various
Python string functions is a must if you want to excel as a Python programmer. You can build your Python skills by attending seminars, reading related blogs,
or taking a Python course to sharpen your programming skills.
Basic String Functions
lower()
The lower() string function returns a given string into a new string where all characters are in lowercase. This specific function simply ignores symbols and
numbers and, thus, has nothing to do with them.
Syntax: “string.lower()” is the syntax of the lower string function.
For example,
string = “Internshala is Working to Equip Students with Relevant Skills and Practical Exposure”
print(string.lower())
Output:
internshala is working to equip students with relevant skills and practical exposure
https://trainings.internshala.com/blog/python-string-functions/ 1/8
8/11/25, 1:36 PM Python String Functions With Examples
Upper()
Home Blogs Data Science Business & Management Programming Online Trainings OFFER Placement Guarantee C
The upper() string function returns a given string into a new string where all characters are in the uppercase. This specific function simply ignores symbols
and numbers and, thus, has nothing to do with them. “string.upper()” is the syntax of the upper string function.
For example,
string = “Internshala is Working to Equip Students with Relevant Skills and Practical Exposure”
print(string.upper())
Output:
INTERNSHALA IS WORKING TO EQUIP STUDENTS WITH RELEVANT SKILLS AND PRACTICAL EXPOSURE
capitalize()
The capitalize() string function returns a given string into a new string where only the first character is in the uppercase and all the remaining characters are in
the lowercase.
Syntax: “string.capitalize()” is the syntax of capitalize string function.
For example,
string = “i am learning the PYTHON Programming Language”
print(string.capitalize())
Output:
I am learning python programming language.
title()
The title() string function returns a given string into a new string where the first character of each word is in the uppercase, and the remaining characters are
in the lowercase.
Syntax: “string.title()” is the syntax of the title string function.
For example,
string = “i am learning PYTHON Programming Language”
print(string.title())
Output:
I Am Learning Python Programming Language
casefold()
Similar to the lower() string function, the casefold() function returns a given string into a new string where all characters are in lowercase. However, the
casefold() function is stronger and is used to perform a case-insensitive comparison.
Syntax: “string.casefold()” is the syntax of the casefold string function.
For example,
string = ““I Am learning PYTHON PROGRAMMING LANGUAGE”
print(string.casefold())
Output:
i am learning python programming language
https://trainings.internshala.com/blog/python-string-functions/ 2/8
8/11/25, 1:36 PM Python String Functions With Examples
swapcase()
Home Blogs Data Science Business & Management Programming Online Trainings OFFER Placement Guarantee C
The swapcase() string function returns an uppercase lettered string into a new string with all letters in the lowercase and vice-versa.
Syntax: “string.swapcase()” is the syntax of the swapcase string function.
For example,
Example 1:
string = “I AM LEARNING PYTHON PROGRAMMING LANGUAGE”
print(string.swapcase())
Output:
i am learning python programming language
Example 2:
string - “i am learning python programming language”
print(string.swapcase())
Output:
I AM LEARNING PYTHON PROGRAMMING LANGUAGE
format()
The Python string format function is used to format the string for console printing.
Syntax: “string.format()” is the syntax of the format string function.
For Example,
string = “I have {an : .2f} Candies!”
print (string.format() (an = 4)
Output:
I have 4 Candies!
Also Read: Python Projects With Source Code
Find and Replace String Functions
encode()
The encode() function is used to encode the string using a specified encoding scheme.
Syntax: “string.encode()” is the syntax of encode string function.
https://trainings.internshala.com/blog/python-string-functions/ 3/8
8/11/25, 1:36 PM Python String Functions With Examples
Home Blogs Data Science Business & Management Programming Online Trainings OFFER Placement Guarantee C
For example,
#Variable Declaration
string = “HELLO”
encode = str.encode()
#Displaying Result
print(“Old value”,str)
print(“Encoded value”,encode)
Output:
old vale HELLO
Encoded value b ‘HELLO’
find()
The find() function is used to find the index of a given substring in a string.
Syntax: “string.find()” is the syntax of the find string function.
For Example,
string = “Python is a fun programming language”
#check the index of ‘fun’
print(string.find(‘fun’))
Output:
12
replace()
The replace() function is used to return a string where a specified value is replaced with another specified value.
Syntax: “string.replace()” is the syntax of replace string function.
For example,
string = “bat ball”
#replacing ‘ba’ with ‘ro’
replaced_string - string.replace(‘ba’ , ‘ro’)
print(replaced_string)
Output:
rot roll
Let’s check out some more find and replace string functions in python with examples:
Function Description Syntax
rfind() The rfind() function is used to search a given string for a specified value and return the last position of where it was found. string.rfind()
index() The index() function returns the first position of the first occurrence of a substring in a string. string.index()
rindex() The rindex() function returns the highest index of the substring inside a string. string.rindex()
https://trainings.internshala.com/blog/python-string-functions/ 4/8
8/11/25, 1:36 PM Python String Functions With Examples
Checks StringHome
Functions
Blogs Data Science Business & Management Programming Online Trainings OFFER Placement Guarantee C
The following table specifies checks string functions:
Function Description Syntax
startswith() The startswith() function returns “True” when the string starts with the given substring, otherwise it returns ‘False.’ string.startswith()
endswith() The endswith() function returns “True” when the string ends with the given substring, otherwise it returns ‘False.’ string.endswith()
isdigit() The isdigit() function returns ‘True’ when all characters in a given string are digits, otherwise it returns ‘False.’ string.isdigit()
isalnum() The isalnum() function returns ‘True’ when all characters in a given string are alphanumeric, otherwise it returns ‘False.’ string.isalnum()
isalpha() The isalpha() function returns ‘True’ when all characters in a given string are alphabets, otherwise it returns ‘False.’ string.isalpha()
isidentifier() The isidentifier() function returns ‘True’ when a given string is valid, otherwise it returns ‘False.’ string.isidentifier()
isdecimal() The isdecimal() function returns ‘True’ when all characters in a given string are decimals, otherwise it returns ‘False.’ string.isdecimal()
isnumeric() The isnumeric() function returns ‘True’ when all characters in a given string are numeric, otherwise it returns ‘False.’ string.isnumeric()
islower() The islower() function returns ‘True’ when all cased characters in a given string are lowercase and there is at least one string.islower()
cased character, otherwise it returns ‘False.’
isprintable() The isprintable() function returns ‘True’ when all characters in a given string are printable, otherwise it returns ‘False.’ string.isprintable()
isupper() The isupper() function returns ‘True’ when all cased characters in a given string are uppercase, otherwise it returns string.isupper()
‘False.’
isspace() The isspace() function returns ‘True’ when there are all whitespace characters in a given string, otherwise it returns string.isspace()
‘False.’
istitle() The istitle() function returns ‘True’ when a given string is title cased and not empty, otherwise it returns ‘False.’ string.istitle()
Also Read: Lambda Functions in Python
Padding and Cleaning String Functions
Following table specifies the various padding and cleaning string functions:
Function Description Syntax
center() The center() function is used to pad a given string with the specified character. string.center()
strip() The strip() function is used to remove both leading and trailing characters from a given string. string.strip()
rstrip() The rstrip() function is used to remove the trailing characters from a given string. string.rstrip()
lstrip() The lstrip() function is used to remove the leading characters from a given string. string.lstrip()
ljust() The ljust() function is used to align a given string to the left using a specified width. string.ljust()
rjust() The rjust() function is used to align a given string to the right using a specified width. string.rjust()
zfill() The zfill() function is used to return a string’s copy with ‘0’ characters padded to the left side of the string. string.zfill()
Python Split and Join String Functions
The following table specifies the various split and join string functions:
Function Description Syntax
join () The join() function is used to return a concatenated string. string.join()
partition() The partition() function is used to split the string on the first occurrence of the separator. string.partition()
rpartition() The rpartition() function is used to split the string into three parts. string.rpartition()
split() The split() function is used to split a string using a specified separator. string.split()
https://trainings.internshala.com/blog/python-string-functions/ 5/8
8/11/25, 1:36 PM Python String Functions With Examples
rsplit() The rsplit()Blogs
Home function
is Data
usedScience
to split a string from &
Business the right using a specified
Management separator. Online Trainings OFFER
Programming string.rsplit()
Placement Guarantee C
splitlines() The splitlines() function is used to split the lines at line boundaries. string.splitlines()
Miscellaneous String Functions
The following table specifies miscellaneous string functions:
Function Description Syntax
format_map() The format_map() function is used to format specified values in a given string using a dictionary. string.format_map()
count() The count() function is used to count the number of occurrences of a substring in a given string. string.count()
translate() The translate() function is used to change the given string using translation mappings. string.translate()
Key Takeaway
Let’s summarize everything we have learned about Python string functions with examples so far.
Python string is defined as the sequence of Unicode characters enclosed in quotation marks.
Several built-in functions are provided by Python to manipulate strings.
The string functions are further categorized into case change, find and replace, checks, padding, and cleaning, split and join, and miscellaneous string
functions.
FAQs
1. What are String functions in Python?
There are several String functions in Python that are used for different purposes like creating a formatted string, returning a new string, replacing string
function, and translating string function.
2. How many Python String functions are available to use?
According to Python 3.11, these are the functions in use:
stringliteral
stringprefix
shortstring
longstring
shortstringitem
longstringitem
shortstringchar
longstringchar
stringescapeseq
3. Is there a replace function in Python?
Yes, there is a replace function in Python that is used to replace a specific phrase with another phrase.
4. How to clean up Strings in Python?
The string strip() method is used to clean up the strings in Python.
5. How many string functions in Python are there?
Python has plenty of built-in string methods for modifying and analyzing strings. Python has 51 string functions, including methods to locate, replace, divide,
join, format, and compare strings, among others. These functions make it easy to work with and handle strings in Python programming.
← Previous Next →
Gaurav Aggarwal
Gaurav Aggarwal heads the SEO team at Internshala. He is an accomplished SEO expert with a keen interest in driving organic
traffic and optimizing website performance. His forte is building high growth strategies, technical SEO, and generating organic
engagement that drives long-term profit.
https://trainings.internshala.com/blog/python-string-functions/ 6/8