String
The sequence of characters enclosed within a single
quote or double quotes…
Multiline string
You can assign a multiple string to a variable by using t
hree quotes
a= (“”” “””)
Looping through strings
Even strings are iterable objects,they contain a sequenc
e of characters
For x in “banana”:
print(x)
String length
To get the length of a string,use the len() function
a=”Hello world”
print(len(a))
Checking String
To check if a certain phrase or character is present in a
string,we can use the keywords in or not in
txt=”Python is a High level language”
if(“High” in txt):
print(“yes”)
Slicing String
You can return a range of characters by using the slic
e [Link] the start index and the end index, separated b
y a colon, to return a part of the string.
A=”Hello world”
print(A[2:5])
Uppercase
A=”Hello world”
print([Link]())
Lowercase
A=”HELLO WORLD”
print([Link]())
Remove whitespace
We can remove a white space
A=” Hello world”
print([Link]())
Replace a string
The replace() method replaces a specified ph
rase with another specified phrase.
A=”Welcome to all”
print([Link](“all”,”Techvolt”))
Split string
The split() method splits a string into a list.
A=”Welcome to all”
print([Link](“,”))
String concatenation
To concatenate, or combine, two strings you c
an use the + operator.
Ex:1
A=”Welcome”
B=”to all”
c=A+” ”+b;
print(C)
Ex:2
age=21
txt=”I’m Nivetha and my age { } ”
print([Link](age))
Format String
The format() method allows you to format sele
cted parts of a string.
quantity=3
items=567
price=50
myorder=”I want to pay {2} dollars for {0} pieces of it
em {1}”
print([Link](quantity,items,price))
Escape character
To insert characters that are illegal in a string, use an
escape character.
An escape character is a backslash \ followed by the
character you want to insert.
txt=”I’m \”Nivi\” my age is \\25”
print(txt)