Welcome to a new chapter of our Python learning series, “Learn Python with me“. Today in this chapter, we will have a detailed look at Python Strings and Characters. If you are already an avid reader of our blog, you might have already encountered this topic in one of our previous chapters, Python Variables and Datatypes. However, we will have a detailed look at how you can use the Python Strings and Characters in this chapter and the next couple of chapters. So this is only part 1 of Python Strings, you will find the subsequent parts in the upcoming chapters.
Link to our complete Python Complete tutorial: Learn Python
How to create Python Strings?
By assigning a group of characters to a variable, you can create a Python String easily. You can enclose this group of characters either in double quotes or single quotes, as shown in the example below.
string1="Learn Python with me Tutorial series"
string2='Learn Python with me Tutorial series'
You can notice that both string1 and string2 will produce the same output as Python identifies the characters enclosed in both double quotes and single quotes as a string.
To create strings with multiple lines, you can make them with triple-double quotes or triple single quotes, as shown in the example below.
string3="""A friend in neeed
is a friend
indeed
"""
string4='''A friend in need
is a friend
indeed'''
You might now wonder how you can include single quotes or double quotes within the string. To add a string with single quotes, you should enclose the entire string with double quotes. And to add a string with double quotes, you should enclose the entire string with single quotes. Look at the examples below.
string1="You can include 'single quotes' like this"
print(string1)
string2='You can include "double quotes" like this'
print(string2)
Output
You can include 'single quotes' like this
You can include "double quotes" like this
Python String Escape Characters
You can also use the escape characters to include additional features. For example, you can use \t or \n to add horizontal space or the next line as shown in the example below.
string1="Welcome to \t Learn Python with me\ntutorial"
print(string1)
Output
Welcome to Learn Python with me
tutorial
Please find below the entire list of Python String Escape Characters
Escape Character | Description |
\\ | Display single \ |
\x | Character x |
\r | Enter button |
\v | Vertical tab space |
\t | Horizontal tab space |
\n | Newline |
\b | Backspace |
\a | Bell or alert |
If you want to display the string with escape characters, you can do so by declaring the string as a raw string as shown in the example below.
string1=r"Welcome to \t Learn Python with me\ntutorial"
print(string1)
Output
Welcome to \t Learn Python with me\ntutorial
How to find the length of the string?
You can find the length of any string using the len() function as shown in the example below.
string1="Learn Python with me"
a=len(string1)
print("Length of string: ",a)
Output
Length of string: 20
Indexing in Strings
A string index refers to the position of each letter in the string. Python uses the square brackets [] for the same. Please refer to the diagram below to understand this concept better.
Let us now look at a program to understand this concept better.
#String indexing example
string1="Learn Python"
#access each character using while loop
length=len(string1)
i=0
while i<length:
print(string1[i],end=' ')
i+=1
print()
#how to access in reverse order
i=1
while i<=length:
print(string1[-i],end=' ')
i+=1
Output
L e a r n P y t h o n
n o h t y P n r a e
Python String Slicing
Just as the name sounds, the slice refers to a part of a string. You can find the format to perform Python slicing below.
stringname[start: stop: stepsize]
If you don’t mention the start or stop, the slicing will be performed from the zeroth element to the (n-1)th element.
#Python string slicing program
string1='Good Action'
a=string1[0:9:1] #string from 0 to 8th element
print(a)
b=string1[0:9:2] #displays every character from 0th, 2nd, 4th, 6th
print(b)
c=string1[::] #string from 0th to last character
print(c)
d=string1[2:4:1] #elements from string1[2] to string1[3] in steps of 1
print(d)
e=string1[::2] #elements from complete string in steps of 2
print(e)
f=string1[2::] #elements from string1[2] till end
print(f)
g=string1[:4:] #elements from string1[0] to string1[3] in steps of 1
print(g)
h=string1[-4:-1] #elements from string1[-4] to string1[-2]
#from left to right in string1
print(h)
i=string1[-6::]
#elements from string1[-6] till end of the string
print(i)
j=string1[-1:-4:-1]
#elements from string1[-1] to string1[-3] from right to left
print(j)
k=string1[-1::-1]
#elements from string[-1] till first element from right to left
print(k)
Output
Good Acti
Go ci
Good Action
od
Go cin
od Action
Good
tio
Action
noi
noitcA dooG
How to repeat the Strings?
‘*’ is the repetition operator and it can be used to repeat any string any number of times. Please refer to the example below to know more details about the same.
#string repeating program
string1="Learn Python"
print(string1*2)
Output
Learn PythonLearn Python
How to concatenate strings?
Using the operator, ‘+’, you can concatenate any strings in Python. Concatenation means joining strings. Let us look at an example below.
#string concatenation program
string1="Learn"
string2="Python"
string3=string1+string2
print(string3)
Output
LearnPython