Strings
Strings
Topic Examples
concatenation,
repetition,
membership
slicing
len()
capitalize() Important point to note - Capitalizes first letter and converts others
to lower case
x ="35 is good"
a = x.capitalize()
print(a)
35 is good
x = "Hi I AM AbhiRUP"
a = x.capitalize()
print(a)
Hi i am abhirup
title() txt =input("input: ")
a = txt.title()
print(a)
input: i am ranveer
I Am Ranveer
input: ran2veer
Ran2Veer
lower(),upper() txt =input("input: ")
a = txt.upper()
b=txt.lower()
print(a)
print(b)
Output:
input: Hello I am abhirup
HELLO I AM ABHIRUP
hello i am abhirup
count() a="heritage_"
b=a.count("e")
i=a.count("e",0,1)
c=a.count("z")
d=a.count("_")
l=a.count("ri")
print(i)
print(b)
print(c)
print(d)
print(l)
0
2
0
1
1
find() print ("This is how you use the find fucntion")
a = str(input("Enter a sentence - "))
if a==int:
print ("Invalid Input")
b = str(input("Enter the word you want to find - "))
x = a.find(b)
print(x)
6
print(variable.index("a",2,8))
8
print(variable.index("z",2))
a = x.endswith(".")
print(a)
print(x.endswith(",",5,6))
print(x.endswith("?"))
Output:
True
True
False
startswith()
if y == True:
print("your string is alphanumeric")
else:
print("your string is not alphanumeric")
enter a string- HelloWorld123
your string is alphanumeric
if y == True:
print("your string is alpha")
else:
print("your string is not alpha")
Output:
enter a string- lol
your string is alpha
enter a string- l ol
your string is not alpha
if y == True:
print("your string is comprised of numbers")
else:
print("your string is not comprised of numbers")
enter a string- 12345
if y == True:
print("your string is in lowercase")
else:
print("your string is not in lowercase")
enter a string- 1
your string is not in lowercase
= RESTART: C:/Users/sp.lab35/Desktop/akshat.py
enter a string- One
your string is not in lowercase
= RESTART: C:/Users/sp.lab35/Desktop/akshat.py
enter a string- one1
your string is in lowercase
= RESTART: C:/Users/sp.lab35/Desktop/akshat.py
enter a string- ONE
your string is not in lowercase
= RESTART: C:/Users/sp.lab35/Desktop/akshat.py
enter a string- one
your string is in lowercase
= RESTART: C:/Users/sp.lab35/Desktop/akshat.py
enter a string- on1e
your string is in lowercase
= RESTART: C:/Users/sp.lab35/Desktop/akshat.py
enter a string-
your string is not in lowercase
= RESTART: C:/Users/sp.lab35/Desktop/akshat.py
enter a string- on*e
your string is in lowercase
if y == True:
print("your string is in uppercase")
else:
print("your string is not in uppercase")
= RESTART: C:/Users/sp.lab35/Desktop/akshat.py
enter a string- Hello
your string is not in uppercase
isspace() x= input(“Enter string-“)
y= x.isspace()
if y == True:
print("your string consists of only spaces")
else:
print("your string doesn't consist of just spaces")
Output:
enter a string-
your string consists of only spaces
= RESTART: C:/Users/sp.lab35/Desktop/akshat.py
enter a string- a
your string doesn't consist of just spaces
= RESTART: C:/Users/sp.lab35/Desktop/akshat.py
enter a string-
your string doesn't consist of just spaces
= RESTART: C:/Users/sp.lab35/Desktop/akshat.py
your string consists of only spaces
lstrip(),rstrip() string = " gourang x mihir x ranveer "
print(str(len(string)))
a = string.lstrip(" gornau x ")
b = string.rstrip()
print(a + ": " + str(len(a)))
print(b + ": " + str(len(b)))
output
47
mihir x ranveer : 23
gourang x mihir x ranveer: 39
print('************************************************************************
******')
print('Case 1')
txt1 = " FORTNITE "
print('************************************************************************
******')
print('Case 2')
txt1 = " FORTNITE "
y = txt1.strip()
print('************************************************************************
******')
replace() text = ('One fish two fish red fish blue fish')
new_text = text.replace('fish','cat', 2)
print(new_text)
text = ('One fish two fish red fish blue fish')
new_text = text.replace('fish','cat')
print(new_text)
text = ('One fish?two fish red fish blue fish')
new_text = text.replace('','*')
print(new_text)
Output:
One cat two cat red fish blue fish
One cat two cat red cat blue cat
*O*n*e* *f*i*s*h*?*t*w*o* *f*i*s*h* *r*e*d* *f*i*s*h* *b*l*u*e*
*f*i*s*h*
join() x=["apples","bananas","oranges"]
y=["kitkat","snickers","cadbury"]
print(x)
result='.'.join(x+y)
print(result)
result='.'.join(x).join(y)
print(result)
output
['apples', 'bananas', 'oranges']
apples.bananas.oranges.kitkat.snickers.cadbury
kitkatapples.bananas.orangessnickersapples.bananas.orangescadb
ury
x='ilikepyhtonfunctions'
y=(x.partition(' '))
print(len(y[2]))
output:
apples bananas oranges
('apples ', 'bananas', ' oranges')
apples
bananas
oranges
0
apples bananas oranges
('', 'apples', ' bananas oranges')
split() x='Hello World. How are we?'
y=x.split()
print(y)
Output
['Hello', 'World.', 'How', 'are', 'we?']
['Hello World', ' How are we?']
['Hello', 'World.', 'How', 'are', 'we?']
chr(), ord()