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.
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
Python3
# 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
Python3
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
Python3
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 links:
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 -> ‘neveropen is a computer science portal’
Python3
message = 'GEEKSFORGEEKS IS A COMPUTER SCIENCE PORTAL' # convert message to lowercase print (message.lower()) |
Output:
neveropen 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: neveropen is a computer science portal -> GEEKSFORGEEKS IS A COMPUTER SCIENCE PORTAL
Python3
message = 'neveropen 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
Python3
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.
Python3
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.