String functions are used in python language to manipulate a string or query information about a string.
These are some string function used in python :-
Table of Contents
Capitalize() function in python
• Capitalize() function in python :- It converts the first character of a string to upper case.
For example :- str = "hello, and welcome to sach educational support
X = str.capitalize()
print(X)
Output :- hello, and welcome to sach educational support.
Casefold() function in python
• Casefold() :- The casefold() method returns a string where all the characters are lower case.
This method is similar to the lower() method, but the casefold() method is stronger, more aggressive, meaning that it will convert more characters into lower case, and will find more matches when comparing two strings and both are converted using the casefold() method.
For example :- str = “Hello, And Welcome To Sach Educational Support!”
x = str.casefold()
print(x)
Output :- hello, and welcome to sach educational support!
Center() function in python
• Center():-The center() method will center align the string, using a specified character (space is default) as the fill character.
Parameter required are the length of the returned string and the character to fill the missing space on each side.
For example :- str = “porwal”
x = str.center(20, “A”)
print(x)
Output :- AAAAAAAporwalAAAAAAA
Count() function in python
• Count :- The count() method returns the number of times a specified value appears in the string.
Parameter required are the string to value to search for, the position to start the search and the position to end the search.
For Ex:- txt = “I love apples, apple are my favorite fruit”
x = txt.count(“apple”, 10, 24)
print(x)
Output:- 1
Encode() function in python
• Encode() function in python :- The encode() method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used.
Parameter required are a String specifying the encoding to use and a String specifying the error method.
For ex :- txt = “My name is Ayush”
print(txt.encode(encoding=”ascii”,errors=”backslashreplace”))
print(txt.encode(encoding=”ascii”,errors=”ignore”))
print(txt.encode(encoding=”ascii”,errors=”namereplace”))
print(txt.encode(encoding=”ascii”,errors=”replace”))
print(txt.encode(encoding=”ascii”,errors=”xmlcharrefreplace”))
Output:- b’My name is Ay\xe5sh
b’My name is Aush
b’My name is Ay\N{LATIN SMALL LETTER A WITH RING ABOVE}sh’
b’My name is Ay?sh’
b’My name is Ayush’
Endswith() Function in python
• Endswith() Function in python:- The endswith() method returns True if the string ends with the specified value, otherwise False.
Parameter required are the value to check if the string ends with.
For example: txt = “Hello, welcome to my world.”
x = txt.endswith(“my world.”)
print(x)
Output :- True
Find() Function in python
• Find() Function in python:- The find() method finds the first occurrence of the specified value.The find() method returns -1 if the value is not found.The find() method is almost the same as the index() method, the only difference is that the index() method raises an exception if the value is not found.
For ex :- txt = “Hello, welcome to my world.”
x = txt.find(“e”)
print(x)
Output:- 1
• Index():- The index() method finds the first occurrence of the specified value.The index() method raises an exception if the value is not found.
Parameter required is the value to search for
For ex:- txt = “Hello, welcome to my world.”
x = txt.index(“e”)
print(x)
Output:- 1
Also see : modules in python
Islower() Function in python
• Islower() Function in python:- The islower() method returns True if all the characters are in lower case, otherwise False.Numbers, symbols and spaces are not checked, only alphabet characters.
For ex : a = “Hello world!”
b = “hello 123”
c = “mynameisAyush”
print(a.islower())
print(b.islower())
print(c.islower())
Output:- False
True
False
• Isupper() :- The isupper() method returns True if all the characters are in upper case, otherwise False.Numbers, symbols and spaces are not checked, only alphabet characters.
For ex:- a = “Hello World!”
b = “hello 123”
c = “MY NAME IS AYUSH”
print(a.isupper())
print(b.isupper())
print(c.isupper())
Output:- False
False
True
• Join:- The join() method takes all items in an iterable and joins them into one string.A string must be specified as the separator.
Parameter required is any iterable object where all the returned values are strings
For ex:- myDict = {“name”: “John”, “country”: “Norway”}
mySeparator = “TEST”
x = mySeparator.join(myDict)
print(x)
Output:- nameTESTcountry
• Lower() :- The lower() method returns a string where all characters are lower case.Symbols and Numbers are ignored.
For ex:- txt = “Hello my FRIENDS”
x = txt.lower()
print(x)
Output :- hello my friends
• lstrip() :- The lstrip() method removes any leading characters (space is the default leading character to remove).
Parameter required is a set of characters to remove as leading characters
For ex:- txt = “,,,,,ssaaww…..banana”
x = txt.lstrip(“,.asw”)
print(x)
Output:- banana
• Partition() :- The partition() method searches for a specified string, and splits the string into a tuple containing three elements.The first element contains the part before the specified string.The second element contains the specified string.The third element contains the part after the string.
Parameter required is the string to search for.
For ex:- txt = “I could eat bananas all day”
x = txt.partition(“apples”)
print(x)
Output: – (‘I could eat bananas all day’, ”, ”)
• Replace :- The replace() method replaces a specified phrase with another specified phrase.
Parameter required is the string to search for, the string to replace the old value with, a number specifying how many occurrences of the old value you want to replace. Default is all occurrences
For ex:- txt = “one one was a race horse, two two was one too.”
x = txt.replace(“one”, “three”)
print(x)
Output:- three three was a race horse, two two was three too.”
• rfind() :- The rfind() method finds the last occurrence of the specified value.The rfind() method returns -1 if the value is not found.The rfind() method is almost the same as the rindex() method.
Parameter required is the value to search for,
For ex: txt = “Hello, welcome to my world.”
x = txt.rfind(“e”)
print(x)
Output: 13
• rsplit():- The rsplit() method splits a string into a list, starting from the right.If no “max” is specified, this method will return the same as the split() method.
For ex: – txt = “apple, banana, cherry”
# setting the maxsplit parameter to 1, will return a list with 2 elements!
x = txt.rsplit(“, “, 1)
print(x)
Output: [‘apple, banana’, ‘cherry’]
• Split() :- The split() method splits a string into a list.
Parameter required is Specifies the separator to use when splitting the string. By default any whitespace is a separator
For ex: – txt = “hello, my name is Peter, I am 26 years old”
x = txt.split(“, “)
print(x)
Output: [‘hello’, ‘my name is Peter’, ‘I am 26 years old’]
• Strip() :- The strip() method removes any leading (spaces at the beginning) and trailing (spaces at the end) characters (space is the default leading character to remove).
Parameter required is A set of characters to remove as leading/trailing characters
For ex :- txt = “,,,,,rrttgg…..banana….rrr”
x = txt.strip(“,.grt”)
print(x)
Output: banana
• upper() :- The upper() method returns a string where all characters are in upper case.
For ex:- txt = “Hello my friends”
x = txt.upper()
print(x)
Output :- HELLO MY FRIENDS
swapcase() Function in python
• swapcase() Function in python :- The swapcase() method returns a string where all the upper case letters are lower case and vice versa.
For ex:- txt = “Hello My Name Is SACH”
x = txt.swapcase()
print(x)
Output:- hELLO mY nAME iS sach
translate() Function in python
• translate() :- The translate() method returns a string where some specified characters are replaced with the character described in a dictionary, or in a mapping table.Use the maketrans() method to create a mapping table.If a character is not specified in the dictionary/table, the character will not be replaced.If you use a dictionary, you must use ascii codes instead of characters.
Parameter required is Either a dictionary, or a mapping table describing how to perform the replace
For ex:- txt = “Hi Sam!”;
x = “mSa”;
y = “eJo”;
mytable = txt.maketrans(x, y);
print(txt.translate(mytable));
Output:- Hi Joe!
Summary :
In this article we saw string functions in python 3.9 so about this section you have any query then free to ask me