STRINGS
STRINGS
strings in python are surronded by either single quotation marks, or double quotation marks.
In [1]:
print("hello")
print('hello')
hello
hello
In [2]:
A ="hello"
print(A)
hello
Multiline strings
You can assign a multine string to variable by using three quotes:
In [3]:
A = """Here we assigned multiple
strings to a variable A"""
print(A)
In [4]:
A = '''Here we assigned multiple
strings to a variable A'''
print(A)
however,python doesnt have a charcter data type,a single charcter is simply a string with length of 1.
In [5]:
#getting the first charcter of the string
x="hello world!"
print(x[0])
In [6]:
x="Bananna"
for i in x:
print(i)
B
a
n
a
n
n
a
string length
To get the length of the string we use "len()" function
In [7]:
x="Bananna"
print(len(x))
In [8]:
#if r is present in the x then it will return True
x="if r is present in the x then it will return True"
print("present" in x)
True
r is in x
Check if NOT
To check if an charcter or pharse is NOT present in the string ,for this we can use "not in"
In [10]:
#it will return either true or false
x="Besant located at jaynagar,rajajaigar,BTM"
print("peenya" not in x)
True
In [11]:
#performing task using the if conditions
x="Besant located at jaynagar,rajajaigar,BTM"
if "peenya" not in x:
print("Besant doesnt have branch in peenya ")
else:
print("not in there")
Specify the start and end index position by separtuing them with a colon to return a part of the string
In [12]:
x="hi Veerendra"
#string[start:end]
print(x[2:7]) #the first charcter has index 0
Veer
In [13]:
x="hi Veerendra"
print(x[:4]) #printing charcters from start to index position 5 and it will ignoe the white space
hi V
slice to end
by leaving the end position of the index ,the range will go the end
In [14]:
x="hi Veerendra"
print(x[2:]) #printing the charcters from index 2 to end point
Veerendra
Negative indexing
In [15]:
x="hi Veerendra"
print(x[-5:-3]) #printing from index -5 to -3
en
STRING METHODS
captalize()
converts the string into captal
casefold()
converts string into lower case
center()
Brings the string into center
count()
Returns the number of times specified chscrcter or string repated
startswith()
Returns true if the string starts with specified string
endswith()
Returns true if string ends with specified cahrcter or string
uppper()
converts the string into upper case
lower()
converts the string into lowercase
islower()
Returns true if all charcters in the string are lower case
isupper()
Returns true if all charcters in the string are upper case
isdigit()
Returns true if all charcters in the string are digits
isnumeric()
Returns true if all charcters in the string are numeric
isdecimal()
Returns true if all charcters in the string are decimals
isalnum()
Returns true if all charcters in the string are alphanumeric
isalpha()
Returns true if all charcters in the string are alphabets
format()
Formats specified values in the string
index()
Seraches the string for a specifed vaue and returns the position of where it was found
rindex()
seraches the string for a specified value and returns the last position of where it was found
split()
Splits the string at the specified separator and returns a list
rsplit()
Splits the string at the specified separator and returns a list
strip()
Return a trimmed version of the string
rstrip()
Return right trim version of the string
find()
Returns the index position of the specified string
rfind()
seraches
replace()
returns the string where specified value is replaced with specified value
Modify strings
python has built in methods that we can use on strings
casefold()
converts the string into lower case
In [16]:
x="HELLO"
y=x.casefold()
print(y)
hello
Capitalize()
It converts the first letter of the string into upper case
In [17]:
x="hello"
y=x.capitalize()
print(y)
Hello
center()
It will bring the string into center and we need to decalare a value to where you want to put your string and the value should be an integer value
In [18]:
x="Avengers"
print(x.center(30)) #here we gave a value to which poistion you want to place the string however it will bring the string center only
Avengers
count()
This function is used to check a specified string or charcter was repated in given string it will retuen an integer value
In [19]:
x="madam"
print(x.count("m")) #you need to give a argument that you need to check
startswith()
This function will true if given string start with specified string or it will retuen false
In [20]:
x="Banglore is the capital city of karnataka"
print(x.startswith("Banglore"))
print(x.startswith("manglore"))
True
False
endswith()
It is smilar to "startswith()" the only diffrence is it checks at end only
In [22]:
x="Banglore is the capital city of karnataka"
print(x.endswith("karnataka"))
print(x.endswith("manglore"))
True
False
upper()
This function will return the string in upper case
In [ ]:
x="hello"
print(x.upper()) #it does'nt take any arguments
isupper()
it will retuen "True" if all the strings are in uppercase or else it wil retuen false
In [ ]:
x="AVENGERS"
print(x.isupper())
lower()
It will convert the string into lowercase
In [ ]:
x="AVENGERS"
print(x.lower())
islower()
it will return true if all the strings are in lowercase
In [ ]:
x="banglore"
print(x.islower())
isdigit()
It will return true if all strings are digits else false
In [ ]:
x="121212"
print(x.isdigit())
isnumeric()
It will return true if all the strings charcters are numbers
In [ ]:
x="454545"
print(x.isnumeric())
isalpha()
Returns true if all the strings are alphabets else false it will return
In [ ]:
x="abcdefghijklmnopqrstuvwxyz"
print(x.isalpha())
isalnum()
it will return true if a string is a combination of numbers and alphabets. else false
In [ ]:
x="187z1a0537"
print(x.isalnum())
isdecimal()
it will return true if strings contain decimal values in it else false it will return
In [ ]:
x="2.36"
print(x.isdecimal())
format()
The format() method formats the specified value(s) and insert them inside the string's placeholder.
In [ ]:
Text = "My name is {0}, I'm {1}".format("John",36)
print(Text)
index()
seraches the string of the specfied value and returns where it was found and it returns the value where it first found and it will ignore remaining
In [ ]:
x="Tony stark is an avenger"
print(x.index("t"))
rindex()
seraches the string of the specfied value and returns where it was found and it returns the value where it last found and it will ignore remaining
In [ ]:
x="Tony stark is an avenger"
print(x.rindex("r"))
split()
Splits the string at the specified separator and returns a list
In [ ]:
x="CIVIL WAR BETWEEN IRONMAN,AND CAPTAINAMERICA"
print(x.split(" "))
rsplit()
it is smilar to the split() the only diffrence is it is mainly used for for rawstring
In [ ]:
x="CIVIL WAR BETWEEN%&IRONMAN,AND CAPTAINAMERICA"
print(x.rsplit(" "))
strip()
It will delete the starting and ending white space and return the string
In [ ]:
x=" hello world" #but it wont remove the midlle white space of the string
print(x.strip())
rstrip()
It will delete the ending white space onlyand return the string
In [ ]:
x=" hello world" #but it wont remove the midlle white space string
print(x.rstrip())
find()
This function will return the index position of the specified string
In [ ]:
x="AVENGERS"
print(x.find("G"))
rfind()
The rfind() method finds the last occurrence of the specified value.
In [ ]:
x="AVENGERS END GAME"
print(x.rfind("GAME"))
replace()
returns the string where specified value is replaced with pecified value
In [ ]:
x="hello ,world"
print(x.replace("l","i")) #it takes two arguments first one which charcter you want to replace and and second one you want to replace.