Strings are collections of characters or we can consider them as a word or characters collected together. Strings can also be called character arrays.
Like other languages, Strings are there in Python also.
Creating Strings in Python:
Strings in Python are contained in either single quotation marks or double quotation marks.
Code:
Python Code
s = "TakeUForward" #initialising a string
print(s) #printing the string
Output: TakeUForward
Strings can contain any character. In Python, anything inside two quotation marks is a string.
Example –
“1234567” , “%&*#$” , etc. are strings.
MultiLine Strings –
Strings can be MultiLine as well. We can do so by using three quotation marks at the start and end of the string.
Code:
Python Code
s = """Hi , Welcome to TakeUForward !!!
We provide coding tutorials and resources for free
""" #initialising a string
print(s) #printing the string
Output:
Hi , Welcome to TakeUForward !!!
We provide coding tutorials and resources for free
Strings As Arrays
Strings are arrays of characters, and we can perform operations on strings, just like arrays. We will see the following operations one by one –
1. Accessing the Characters of the String
2. Looping through the String
Accessing the Characters of a string:
We can use square brackets[] to access the characters one by one. Accessing the zeroth element will actually give us the first letter of the string because Python uses zero-based indexing. We can also use -ve indexing in Python. If we use -1 or -2, i.e., we want to access the last or second last character of the string. But if we try to access an element that is out of range, then it will throw an IndexError. And we can access only using the integers. Using float etc. will throw a TypeError.
Code:
Python Code
s = "TakeUForward"
print(s[0]) # will print the first character of string s
print(s[3]) # will print the 4th character of string s
Output:
T
e
Looping through the String
We can also loop through the characters of a string, as we do in arrays. We can do it, by using any of the for, while, do-while loops.
Code:
Python Code
s = "TakeUForward"
for x in s:
print(x) #this for loop will print all the characters present in the string s
Output:
T
a
k
e
U
F
o
r
w
a
r
d
Length of the String –
We have inbuilt functions for finding the length of the string. We can also use for loop, and counting for each character.
Syntax:
len(string)
Code:
Python Code
s = "TakeUForward"
print(len(s))
Output: 12
String Slicing
String Slicing is a very important technique in Python. To access a particular range of characters, we can use slicing.
Slicing can be done by using a colon (:).
Code:
Python Code
s = "TakeUForward"
print(s) # initial string
#now slicing the string from some index to other......like creating a substring
print("\nslicing and printing the string from 4th to 9th character")
print(s[4:9]) # this will print the characters present between 4th character and
#9th character
print("\nslicing and printing the string from 4th to 2nd last character")
print(s[4:-2]) #this will print the characters present between 4th character and
#second last character
Output:
TakeUForward
slicing and printing the string from 4th to 9th character
UForw
slicing and printing the string from 4th to 2nd last character
UForwa
Deletion/Updation of a String
In Python, we can’t delete or update characters from a string. This is because Strings are immutable in Python, hence elements can not be changed, once assigned. New Strings can be reassigned to the same name. If we try to update or delete characters from a string, it will give an error.
We can delete an entire word by using the del keyword.
Escape Sequencing in Python –
If we want to use single quotes or double quotes in a string, then we need to make use of escape sequencing. We need to use ‘\’ a backslash before every single or double quote. If we don’t do escape sequencing, then, the compiler won’t be able to differentiate between the ending quote and the quote in between, and it will create ambiguity and will throw an error.
Example:
s = "I\'m Studying" #adding backslash before the quotation marks print(s)
String Formatting
If we want to format strings in Python, we can use format(). It is a very powerful method for formatting strings.
In this method, we use curly braces {}, to hold the arguments and specify the order of position.
Code:
Python Code
s = "{}{}{}".format('i ', 'love ' , 'coding ')
print(s) # printing a simple string
#now changing the order of string
s = "{2} {1} {0}".format('i', 'love' , 'coding')
print(s) #now this will print "coding love i" , in the specified order.
#now again changing the order of string
s = "{2} {0} {1}".format('i', 'love' , 'coding')
print(s) #now this will print "coding i love" , in the specified order.
Output:
i love coding
coding love i
coding i love
Special thanks to Ishita Dhiman for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article. If you want to suggest any improvement/correction in this article please mail us at [email protected]