String Manipulation in Python

Strings are sequences of characters. There are numerous algorithms for processing strings, including for searching, sorting, comparing and transforming. Python strings are "immutable" which means they cannot be changed after they are created . To create a string, put the sequence of characters inside either single quotes, double quotes, or triple quotes and then assign it to a variable.
str = 'Hellow World!' str = "Hellow World!" str = """Sunday Monday Tuesday"""
Access characters in a string

In order ot access characters from String, use the square brackets [] for slicing along with the index or indices to obtain your characters. Python String index starts from 0.

str = 'Hellow World!' print(str [0]) # output is "H" print(str [7]) # output is "W" print(str [0:6]) #output is Hellow print(str [7:12]) #output is World

Python allows negative indexing for its sequences.

The index of -1 refers to the last item, -2 to the second last item and so on.

print(str [0:-6]) # output is Hellow print(str [7:-1]) # output is World
String Concatenation

Joining of two or more strings into a single one is called concatenation. Python uses "+" operator for joining one or more strings

str1 = 'Hellow ' str2 = ' World!' print(str1 + str2)
output
Hellow World!
Reverse a String

In Python Strings are sliceable. Slicing a string gives you a new string from one point in the string, backwards or forwards, to another point, by given increments. They take slice notation or a slice object in a subscript:

string[subscript]

The subscript creates a slice by including a colon within the braces:

string[begin:end:step]

It works by doing [begin:end:step] - by leaving begin and end off and specifying a step of -1, it reverses a string.

str = 'Python String' print(str[::-1])
output
gnirtS nohtyP
String Methods

Python has several built-in methods associated with the string data type. These methods let us easily modify and manipulate strings. Built-in methods are those that are defined in the Python programming language and are readily available for us to use. Here are some of the most common string methods.

Python String len() method

String len() method return the length of the string.

str = "Hellow World!" print(len(str))
output
13
Python String count() method

String count() method returns the number of occurrences of a substring in the given string.

str = "Python is Object Oriented" substr = "Object" print(str.count(substr)) # return 1, becasue the word Object exist 1 time in str
output
1
Python String index() method

String index() method returns the index of a substring inside the given string.

index(substr,start,end)

end(optional) by default its equal to the length of the string.

str = "Python is Object Oriented" substr = "is" print(str.index(substr))
output
7
Python String upper() method

String upper() convert the given string into Uppercase letters and return new string.

str = "Python is Object Oriented" print(str.upper())
output
PYTHON IS OBJECT ORIENTED
Python String lower() method

String lower() convert the given string into Lowercase letters and return new string.

str = "Python is Object Oriented" print(str.lower())
output
python is object oriented
Python String startswith() method

String startswith() method returns Boolean TRUE, if the string Starts with the specified substring otherwise, it will return False.

str = "Python is Object Oriented" print(str.startswith("Python")) print(str.startswith("Object"))
output
True False
Python String endswith() method

String endswith() method returns Boolean TRUE, if the string Ends with the specified substring otherwise, it will return False.

str = "Python is Object Oriented" print(str.endswith("Oriented")) print(str.endswith("Object"))
output
True False
Python String split() method

String split() method break up a string into smaller strings based on a delimiter or character.

str = 'Python is Object Oriented' print(str.split())
output
['Python', 'is', 'Object', 'Oriented']
example
str = 'Python,is,Object,Oriented' print(str.split(','))
output
['Python', 'is', 'Object', 'Oriented']
example
str = 'Python,is,Object,Oriented' print(str.split(',',2))
output
['Python', 'is', 'Object,Oriented']

Python returned split string as a List

str = 'Python,is,Object,Oriented' sList = str.split(',') for temp in sList: print (temp)
output
Python is Object Oriented
Python String join() method

String join() is a string method which returns a string concatenated with the elements of an iterable.

str = "Python is Object Oriented" tmp = "-" print (tmp.join(str))
output
P-y-t-h-o-n- -i-s- -O-b-j-e-c-t- -O-r-i-e-n-t-e-d
Python String find() method

String find() return the index position of the first occurrence of a specified string. It will return -1, if the specified string is not found.

str = "Python is Object Oriented" st = "Object" print (str.find(st)) print (str.find(st,20)) #finding from 20th position
output
10 -1
Python String strip() method

String strip() remove the specified characters from both Right hand side and Left hand side of a string (By default, White spaces) and returns the new string.

str = " Python is Object Oriented " print (str.strip())
output
Python is Object Oriented
Python String rstrip() method

String rstrip() returns a copy of the string with trailing characters removed.

str = " Python is Object Oriented " print (str.rstrip())
output
Python is Object Oriented
Python String lstrip() method

String lstrip() returns a copy of the string with leading characters removed.

str = " Python is Object Oriented " print (str.lstrip())
output
Python is Object Oriented