7 Useful String Functions in Python
Last Updated :
11 Sep, 2024
As one of the most popular programming languages, Python enables developers to use string functions that help them to perform various operations. For those who don't know, the string data carries 1 or 1> value (that can be any number, unique character, etc.) and later gets converted into ASCII code i.e. American Standard Code for Information Interchange and Unicode so that the machine can understand in their own language.
Python uses the same pattern for its string data which carries out to perform different tasks such as shifting Upper or Lower case, etc. This creates more usefulness among developers to save their time on different tasks without worrying about small typos errors and that's why it is sincerely important for you to have technical knowledge of those string functions that have been narrowed down in this article.
Master Python from beginner to advanced with GeeksforGeeks' comprehensive course! Perfect for aspiring developers, this course covers everything from basic syntax to advanced concepts, helping you become proficient in Python programming.
7 Useful String Functions in Python

1. Capitalize
The capitalize() is used in Python where the first letter of the string is converted into UPPERCASE and the rest of the characters remain the same. On the other hand, if all the characters are in UPPERCASE then the string will return the same value (except the first character).
Example: mY name is YUVRAJ -> My name is yuvraj
Python
# input from users
sentence_1 = "mY name is YUVRAJ"
sentence_2 = "MY name is Ansul"
# Convert case using capitalize()
capitalized_string = sentence_1.capitalize()
print("Sentence 1 output -> ", capitalized_string)
capitalized_string = sentence_2.capitalize()
print("Sentence 2 output -> ", capitalized_string)
Output:
Sentence 1 output -> My name is yuvraj
Sentence 2 output -> My name is ansul
2. Count
The count() is used in Python to count the number of occurrences of an individual element or substring that appears within the string. The count() throws the numeric value that provides the detail of an actual count of a given string.
Example: GFG KARLO HO JAYEGA -> Count of 'G' = 3
Python
message = 'GFG KARLO HO JAYEGA'
# number of occurrence of 'G'
print('Number of occurrence of G:', message.count('G'))
Output:
Number of occurrence of G: 3
3. Find
The find() is used in Python to return the lowest index value from the first occurrence of a string (only in case if its found): else the value would be -1.
Example: Yuvraj is my name -> Position of 'is' = 7
Python
message = 'Yuvraj is my name'
# check the index of 'is'
print(message.find('is'))
Output:
7
Note: If you are confused about how to start learning python, then must refer to the below link:
4. Lower
The lower() is used in Python programming to ensure that all the UPPERCASE characters in the string are converted into lowercase and fetched with a new lowercase string and the original copy of the string remains intact.
Example: GEEKSFORGEEKS IS A COMPUTER SCIENCE PORTAL -> 'geeksforgeeks is a computer science portal'
Python
message = 'GEEKSFORGEEKS IS A COMPUTER SCIENCE PORTAL'
# convert message to lowercase
print(message.lower())
Output:
geeksforgeeks is a computer science portal
5. Upper
The upper() is used in Python programming to ensure that all the lowercase characters in the string are converted into UPPERCASE and fetched with a new string whereas the original copy of the string remains intact.
Example: geeksforgeeks is a computer science portal -> GEEKSFORGEEKS IS A COMPUTER SCIENCE PORTAL
Python
message = 'geeksforgeeks is a computer science portal'
# convert message to uppercase
print(message.upper())
Output:
GEEKSFORGEEKS IS A COMPUTER SCIENCE PORTAL
6. Replace
The replace() is used in Python to replace any unwanted character or text and replace it with the new desired output within the string. The replace() can be used in Python with the below-mentioned syntax to perform the action:
string.replace(old, new, count)
Example: subway surfer -> Replace 's' with 't' = tubway turfer
Python
text = 'subway surfer'
# replace s with t
replaced_text = text.replace('s', 't')
print(replaced_text)
Output:
tubway turfer
7. Join
The join() is used in Python programming to merge each element of an iterable such as a list, set, etc., and later you can use a string separator to separate the values. Thus, join() returns a concatenated string and it will throw a TypeError exception if the iterable contains any non-string element within it.
Python
text = ['Anshul', 'is', 'my', 'only', 'friend']
# join elements of text with space
print(' '.join(text))
Output:
Anshul is my only friend
Note: If you wish to know more about Python Strings, we recommend you to refer this link: Python String Tutorial.
Similar Reads
Must Know Things to Clear Your Python Coding Interview
Python is not only one of the most loved languages but rather it is used in various different domains. Due to its high dependency and usage in a lot of fields, there has suddenly been an increase in the job openings in Python programming language and related frameworks. Python developers are in high
6 min read
Python String
A string is a sequence of characters. Python treats anything inside quotes as a string. This includes letters, numbers, and symbols. Python has no character data type so single character is a string of length 1.Pythons = "GfG" print(s[1]) # access 2nd char s1 = s + s[0] # update print(s1) # printOut
6 min read
Numpy - String Functions & Operations
NumPy String functions belong to the numpy.char module and are designed to perform element-wise operations on arrays. These functions can help to handle and manipulate string data efficiently.Table of ContentString OperationsString Information String Comparison In this article, weâll explore the var
5 min read
Python str() function
The str() function in Python is an in-built function that takes an object as input and returns its string representation. It can be used to convert various data types into strings, which can then be used for printing, concatenation, and formatting. Letâs take a simple example to converting an Intege
3 min read
Output of Python programs | Set 7
Prerequisite - Strings in Python Predict the output of the following Python programs. These question set will make you conversant with String Concepts in Python programming language. Program 1Python var1 = 'Hello Geeks!' var2 = "GeeksforGeeks" print "var1[0]: ", var1[0] # statement 1 print "var2[1:5
3 min read
Python String Methods
Python string methods is a collection of in-built Python functions that operates on strings.Note: Every string method in Python does not change the original string instead returns a new string with the changed attributes. Python string is a sequence of Unicode characters that is enclosed in quotatio
5 min read
Collections.UserString in Python
Strings are the arrays of bytes representing Unicode characters. However, Python does not support the character data type. A character is a string of length one. Example: Python3 # Python program to demonstrate # string # Creating a String # with single Quotes String1 = 'Welcome to the Geeks World'
2 min read
How to change any data type into a String in Python?
In Python, it's common to convert various data types into strings for display or logging purposes. In this article, we will discuss How to change any data type into a string. Using str() Functionstr() function is used to convert most Python data types into a human-readable string format. It is the m
2 min read
Convert integer to string in Python
In this article, weâll explore different methods for converting an integer to a string in Python. The most straightforward approach is using the str() function.Using str() Functionstr() function is the simplest and most commonly used method to convert an integer to a string.Pythonn = 42 s = str(n) p
2 min read
Python string length
The string len() function returns the length of the string. In this article, we will see how to find the length of a string using the string len() method.Example:Pythons1 = "abcd" print(len(s1)) s2 = "" print(len(s2)) s3 = "a" print(len(s3))Output4 0 1 String len() Syntaxlen(string) ParameterString:
4 min read