Python String Methods
About Python String Methods
Python provides a variety of string methods that allow you to manipulate and work with strings.
capitalize() , upper(), lower(), title(), strip(), replace(), startswith(), endswith(), join(), find(), etc. are the most common string methods.
len() Method
len() method returns the length of the string.
Example of len() method:
Output of the above example
The length of the string using len() function: 16
str.capitalize() Method
capitalize() method returns a copy of the string with its first character capitalized and the rest lowercased.
Example of str.capitalize() method:
Output of the above example
Python is great!
str.upper() Method
upper() method returns a copy of the string with all characters converted to uppercase
Example of str.upper() method:
Output of the above example
PYTHON IS GREAT!
str.lower() Method
lower() method returns a copy of the string with all characters converted to lowercase
Example of str.lower() method:
Output of the above example
python is great!
str.title() Method
title() method returns a titlecased version of the string where each word starts with an uppercase character and the remaining characters are lowercase.
Example of str.title() method:
Output of the above example
Python Is Great!
str.strip() Method
strip() method removes leading and trailing whitespaces of the string.
Example of str.strip() method:
Output of the above example
PYTHON IS GREAT!
str.replace(old, new) Method
replace(old, new) method returns a copy of the string replacing all occurrences of the old substring with the new substring.
Example of str.replace() method:
Output of the above example
PYTHON IS AWESOME!
str.split(separator) Method
split(separator) method splits the string into a list of substrings using the specified separator.
Example of str.split() method:
Output of the above example
['PYTHON', ' GREAT!', ' AWESOME']
str.join(iterable) Method
join(iterable) method concatenates (joins) the elements of an iterable (e.g. a list) into a single string using a new string as separator.
Example of str.join() method:
Output of the above example
PYTHON, GREAT!, AWESOME
str.startswith(prefix) Method
startswith(prefix) method checks if the string starts with the specified prefix (substring). This method is case-sensitive.
Example of str.startswith() method:
Output of the above example
False
str.endswith(suffix) Method
endswith(suffix)) method checks if the string ends with the specified suffix (substring). This method is case-sensitive.
Example of str.endswith() method:
Output of the above example
True
str.find(substring) Method
find(substring) method return the index of the first occurrence of the specified substring. It returns '-1' if the substring is not found. This method is case-sensitive.
Example of str.find() method:
Output of the above example
10
Python Compiler For Live Practice
Practice the above Python code in the following Python Editor. Create more Python string variables and perform different string operations in the following Python code editor and click Execute button. Output will be displayed in Python Code Output section.
Python Editor