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 = [Link]()
print(a)
35 is good
x = "Hi I AM AbhiRUP"
a = [Link]()
print(a)
Hi i am abhirup
title() txt =input("input: ")
a = [Link]()
print(a)
input: i am ranveer
I Am Ranveer
input: ran2veer
Ran2Veer
lower(),upper() txt =input("input: ")
a = [Link]()
b=[Link]()
print(a)
print(b)
Output:
input: Hello I am abhirup
HELLO I AM ABHIRUP
hello i am abhirup
count() a="heritage_"
b=[Link]("e")
i=[Link]("e",0,1)
c=[Link]("z")
d=[Link]("_")
l=[Link]("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 = [Link](b)
print(x)
This is how you use the find fucntion
Enter a sentence - Akshat is the best
Enter the word you want to find - Akshat
0
= RESTART: C:/Users/sp.lab35/Desktop/bhavya [Link]
This is how you use the find fucntion
Enter a sentence - Akshat
Enter the word you want to find - kshat
1
= RESTART: C:/Users/sp.lab35/Desktop/bhavya [Link]
This is how you use the find fucntion
Enter a sentence - Adit is so smart
Enter the word you want to find - s
7
= RESTART: C:/Users/sp.lab35/Desktop/bhavya [Link]
This is how you use the find fucntion
Enter a sentence - smart slow sigma smash
Enter the word you want to find - m
1
= RESTART: C:/Users/sp.lab35/Desktop/bhavya [Link]
This is how you use the find fucntion
Enter a sentence - smart slow sigma smash
Enter the word you want to find - sigma
11
= RESTART: C:/Users/sp.lab35/Desktop/bhavya [Link]
This is how you use the find fucntion
Enter a sentence - smart slow sigma smash
Enter the word you want to find - akshat
-1
index() variable = "apple,orange,banana"
print([Link]("orange"0
SyntaxError: '(' was never closed
variable = "apple,orange,banana"
print([Link]("orange")
SyntaxError: multiple statements found while compiling a single
statement
print([Link]("orange"))
6
print([Link]("a",2,8))
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
print([Link]("a",2,8))
ValueError: substring not found
print([Link]("a",2,5))
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
print([Link]("a",2,5))
ValueError: substring not found
print([Link]("a",2))
8
print([Link]("z",2))
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
print([Link]("z",2))
ValueError: substring not found
endswith() x = "Hello, This is a sentence."
a = [Link](".")
print(a)
print([Link](",",5,6))
print([Link]("?"))
Output:
True
True
False
startswith()
isalnum() x= input("enter a string- ")
y= [Link]()
if y == True:
print("your string is alphanumeric")
else:
print("your string is not alphanumeric")
enter a string- HelloWorld123
your string is alphanumeric
enter a string- Hello World123
your string is not alphanumeric
enter a string- Hello_world123
your string is not alphanumeric
enter a string- 123
your string is alphanumeric
isalpha() x= input("enter a string- ")
y= [Link]()
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- l0l
your string is not alpha
enter a string- l ol
your string is not alpha
enter a string- LOL
your string is alpha
enter a string- Lol
your string is alpha
isdigit() x= input("enter a string- ")
y= [Link]()
if y == True:
print("your string is comprised of numbers")
else:
print("your string is not comprised of numbers")
enter a string- 12345
your string is comprised of numbers
enter a string- 17/7/24
your string is not comprised of numbers
enter a string- lol123
your string is not comprised of numbers
enter a string- 1.2
your string is not comprised of numbers
islower(),isupper x= input("enter a string- ")
(), y= [Link]()
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/[Link]
enter a string- One
your string is not in lowercase
= RESTART: C:/Users/sp.lab35/Desktop/[Link]
enter a string- one1
your string is in lowercase
= RESTART: C:/Users/sp.lab35/Desktop/[Link]
enter a string- ONE
your string is not in lowercase
= RESTART: C:/Users/sp.lab35/Desktop/[Link]
enter a string- one
your string is in lowercase
= RESTART: C:/Users/sp.lab35/Desktop/[Link]
enter a string- on1e
your string is in lowercase
= RESTART: C:/Users/sp.lab35/Desktop/[Link]
enter a string-
your string is not in lowercase
= RESTART: C:/Users/sp.lab35/Desktop/[Link]
enter a string- on*e
your string is in lowercase
x= input("enter a string- ")
y= [Link]()
if y == True:
print("your string is in uppercase")
else:
print("your string is not in uppercase")
enter a string- HELLO
your string is in uppercase
= RESTART: C:/Users/sp.lab35/Desktop/[Link]
enter a string- Hello
your string is not in uppercase
isspace() x= input(“Enter string-“)
y= [Link]()
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/[Link]
enter a string- a
your string doesn't consist of just spaces
= RESTART: C:/Users/sp.lab35/Desktop/[Link]
enter a string-
your string doesn't consist of just spaces
= RESTART: C:/Users/sp.lab35/Desktop/[Link]
your string consists of only spaces
lstrip(),rstrip() string = " gourang x mihir x ranveer "
print(str(len(string)))
a = [Link](" gornau x ")
b = [Link]()
print(a + ": " + str(len(a)))
print(b + ": " + str(len(b)))
output
47
mihir x ranveer : 23
gourang x mihir x ranveer: 39
strip() print('Definition of strip -')
print('The Strip() method in Python removes or truncates (The
truncate() method resizes the file to the given number of bytes. If the
size is not specified, the current position will be used) the given
characters from the beginning and the end of the original string. The
default behavior of the strip() method is to remove the whitespace
from the beginning and at the end of the string.')
print('************************************************************************
******')
print('Case 1')
txt1 = " FORTNITE "
print("The best game of all time is easily,", txt1)
print('************************************************************************
******')
print('Case 2')
txt1 = " FORTNITE "
y = [Link]()
print("The best game of all time is easily,",y)
print('************************************************************************
******')
replace() text = ('One fish two fish red fish blue fish')
new_text = [Link]('fish','cat', 2)
print(new_text)
text = ('One fish two fish red fish blue fish')
new_text = [Link]('fish','cat')
print(new_text)
text = ('One fish?two fish red fish blue fish')
new_text = [Link]('','*')
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']
[Link]
[Link]
ury
=== Code Execution Successful ===
partition() x="apples bananas oranges"
print(x)
y =[Link]('bananas')
print(y)
print(y[0])
print(y[1])
print(y[2])
x='ilikepyhtonfunctions'
y=([Link](' '))
print(len(y[2]))
x="apples bananas oranges"
print(x)
y =[Link]('apples')
print(y)
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=[Link]()
print(y)
x1='Hello World. How are we?'
y1=[Link]('.')
print(y1)
x1='Hello World. How are we?'
y1=[Link](' ',2)
print(y1)
Output
['Hello', 'World.', 'How', 'are', 'we?']
['Hello World', ' How are we?']
['Hello', 'World.', 'How', 'are', 'we?']
chr(), ord()