You are on page 1of 5

Experiment No 5

1.Write A Program To Implement Following String


Manipulation Function in Python.
 Creating A String.
mystring = "Hello"
>>> print(mystring)

OUTPUT:
Hello

 Indexing In A String.
mystr = "python"
print("String is ",mystr)

#First Character
print("First Character = ",mystr[0])

#Last Character
print("Last Character = ",mystr[-1])

#Reverse
print("Reversed String =",mystr[::-1])

OUTPUT:
String is python
First Character = p
Last Character = n
Reversed String = nohtyp
 To Delete A String
mystring ="Hello World"
del mystring
print(mystring)

OUTPUT:
NameError: name 'mystring' is not defined

 String Manipulation Functions.


 Concentration Of Two Or More Strings.
str1="Hello"
str2="World!"

#Using +
print('str1 + str2=',str1 + str2)

#Using *
print('str1 * 3=',str1 * 3)

OUTPUT:
str1 + str2= HelloWorld!
str1 * 3= HelloHelloHello

 Iterating Through String.


count=0
for letter in "Hello World":
if(letter == 'l'):
count +=1
print(count,'Letters Found')

OUTPUT:
3 Letters Found
 String Membership Test.
>>> 'a' in 'program'
True
>>> 'at' not in 'battle'
False

 Built-in Function
#Python Program To Demostrate The Use Of len() Method.

#Length Of Below String Is 5


string = "geeks"
print(len(string))

#Length Of Below String Is 15


string ="geeks for geeks"
print(len(string))

OUTPUT:
5
15

Finding.
>>> word ="Hello World"
>>> print(word.count('l'))
#Count How Many Times l Is In The String

OUTPUT:
3
>>>print(word.find("H"))
#Find The Word H In The String.
OUTPUT:
0

>>> print(word.index("World"))
#Find The Letters World In The String.

OUTPUT:
6

 COUNT.
s="Count,The Number Of Spaces"
print(s.count(" "))

OUTPUT:
3

 Split Strings.
word="Hello World"
print(word.split(' '))

OUTPUT:
['Hello', 'World']

 Startswith/EndsWith.
 Replacing
word ="Hello World"
print(word.replace("Hello","Goodbye"))
 OUTPUT:
Goodbye World

 Changing Upper And Lower Case Strings


 F
 s

You might also like