Python String Methods for String Manipulation

String Manipulation is the most essential skill when you are analyzing text data. Python has many built in methods for string manipulation.  In this article, we will study the most frequently used python string methods for string manipulation.

Python String Method to Capitalize a word

To capitalize the first letter of a string in python we use capitalize() method. capitalize() method returns a new string in which the first letter of the string is capitalized. In this process, No change is made to the original string.

Example:

myString="python"
print("Original String:")
print(myString)
newString=myString.capitalize()
print("New modified string:")
print(newString)
print("original string after modification:")
print(myString)

Output:

Original String:
python
New modified string:
Python
original string after modification:
python

In the output, we can see that first letter of the new string has been modified but no changes have been made to string on which the method was invoked.

Python String Method to capitalize first character of each word

To convert first character of each word into capital letter, we can use title() method. When invoked on a string, it capitalizes first character of each word of input string and returns a new string with the result. It doesn’t affects the original string.

Example:

myString="Python is a great language" 
newString=myString.title()
print("Original string is:")
print(myString)
print("Output is:")
print(newString)

Output:

Original string is:
Python is a great language
Output is:
Python Is A Great Language

How to convert string into lowercase in Python?

casefold() method returns a new string when invoked on a python string and turns every letter of original string into lowercase. It doesn’t change the original string. This python string method can be used for preprocessing the text if it contains irregular use of capital or small letters.

Example:

myString="PytHon"
print("Original String:")
print(myString)
newString=myString.casefold()
print("New modified string:")
print(newString)
print("original string after modification:")
print(myString)

Output:

Original String:
PytHon
New modified string:
python
original string after modification:
PytHon

Another method to convert strings into lowercase is the lower() method. It also coverts letters in the text string into lowercase and returns a new string.

Example:

myString="PytHon"
print("Original String:")
print(myString)
newString=myString.lower()
print("New modified string:")
print(newString)
print("original string after modification:")
print(myString)

Output:

Original String:
PytHon
New modified string:
python
original string after modification:
PytHon

How to convert Strings into uppercase in Python?

We can use upper() method to convert an input string into uppercase. When the upper() method is invoked on any string, it returns a new string with all the letter capitalized. It doesn’t change the original string.

Example:

myString="PytHon"
print("Original String:")
print(myString)
newString=myString.upper()
print("New modified string:")
print(newString)
print("original string after modification:")
print(myString)

Output:

Original String:
PytHon
New modified string:
PYTHON
original string after modification:
PytHon

There is another method named swapcase() which swaps the case of every letter in the input string and returns a new string. It doesn’t make any change to the input string on which it is invoked.

Example:

myString="PytHon"
print("Original String:")
print(myString)
newString=myString.swapcase()
print("New modified string:")
print(newString)
print("original string after modification:")
print(myString)

Output:

Original String:
PytHon
New modified string:
pYThON
original string after modification:
PytHon

How to Split Strings in python?

To split strings in python, we use split() method. Python split method takes an optional separator and splits the input string at the points where a separator is present and returns a list containing the splitted parts of the string.

Example:

myString="I am A Python String"
print("Original String:")
print(myString)
newList=myString.split()
print("New List:")
print(newList)
print("when 'A' is declared as separator:")
aList=myString.split("A")
print(aList)
print("original string after modification:")
print(myString)

Output:

Original String:
I am A Python String
New List:
['I', 'am', 'A', 'Python', 'String']
when 'A' is declared as separator:
['I am ', ' Python String']
original string after modification:
I am A Python String

If we want to split a string for certain number of times, we can use rsplit() method instead of split() method. rsplit() method takes an extra argument named maxsplit which is the number of times the string has to be split. The input string is split at maxsplit places from right side of the string and a list with maxsplit+1 fragments of the input string is returned by the rsplit() method. If no value is passed to maxsplit argument, the rsplit() method works the same as split() method.

Example:

myString="I am A Python String"
print("Original String:")
print(myString)
newList=myString.rsplit()
print("New List without maxsplit:")
print(newList)
print("when maxsplit is set at 2:")
aList=myString.rsplit(maxsplit=2)
print(aList)
print("original string after modification:")
print(myString)

Output:

Original String:
I am A Python String
New List without maxsplit:
['I', 'am', 'A', 'Python', 'String']
when maxsplit is set at 2:
['I am A', 'Python', 'String']
original string after modification:
I am A Python String

How to Concatenate Strings in Python?

Now that we have seen how to split the strings, it may be a case that we need to perform string concatenation in python. We can concatenate two strings using "+" operator as well as using join() method.

While using "+" operator, we just add the different strings using "+" operator and assign it to a new string. Here we can concatenate any number of strings in single statements by using "+" operator between them.

Example:

myString1="I am a "
print ("first string is:")
print(myString1)
myString2="Python String"
print("Second String is:")
print(myString2)
myString=myString1+myString2
print("Conactenated string is:")
print(myString)

Output:

myString1="I am a "
print ("first string is:")
print(myString1)
myString2="Python String"
print("Second String is:")
print(myString2)
myString=myString1+myString2
print("Conactenated string is:")
print(myString)

We can also concatenate strings using join method in python. Join method is invoked on a string which is separator and a list or any other iterable of strings is passed to it for joining. It returns a new string containing words in the iterable, separated by the separator string.

Example:

myStringList=["I","am","a","python","string"]
print ("list of string is:")
print(myStringList)
separator=" "#space is used as separator
myString=separator.join(myStringList)
print("Concatenated string is:")
print(myString)

Output:

list of string is:
['I', 'am', 'a', 'python', 'string']
Concatenated string is:
I am a python string

How to trim Strings in Python?

There may be chances that strings are containing extra spaces in the start or end. We can remove those spaces using python string methods namely strip()lstrip() and rstrip().

lstrip() method removes spaces from start of the input string and returns a new string.

rstrip() removes spaces from the end of the string and returns a new string.

strip() method removes spaces from both the start and end of the input string and returns a new string.

Example:

myString="          Python          " 
lstring=myString.lstrip()
rstring=myString.rstrip()
string =myString.strip()
print("Left Stripped string is:",end="")
print(lstring)
print("Right Stripped string is:",end="")
print(rstring)
print("Totally Stripped string is:",end="")
print(string)

Output:

Left Stripped string is:Python          
Right Stripped string is:          Python
Totally Stripped string is:Python

Python String Methods to split a string at newlines.

By using splitlines() method in python, we can convert a string into a list of sentences. The function splits the input string at linebreaks or newlines and returns a new list containing all the fragments of the input string.

Example:

myString="Python is a great language.\n I love python" 
slist=myString.splitlines()
print("Original string is:")
print(myString)
print("Output is:")
print(slist)

Output:

Original string is:
Python is a great language.
I love python
Output is:
['Python is a great language.', ' I love python']

Conclusion

In this article, we have seen python string methods to manipulate the string data in python. We have seen how to split, strip, and concatenate the strings using different methods. We have also seen how to change case of letters in the strings. Stay tuned for more informative articles.

Leave a Reply

Your email address will not be published. Required fields are marked *