Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
50 views5 pages

Experiment No 5: 1.write A Program To Implement Following String Manipulation Function in Python

The document describes various string manipulation functions in Python including: 1. Creating, indexing, and reversing strings. 2. Concatenating strings using + and repeating strings using *. 3. Iterating through strings to count characters, membership testing using 'in', and using built-in functions like len() to find length. 4. Additional functions covered include count(), find(), split(), startswith/endswith, replace(), changing case, and more. Examples are provided to demonstrate the output of each function.

Uploaded by

Bharat Malviya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views5 pages

Experiment No 5: 1.write A Program To Implement Following String Manipulation Function in Python

The document describes various string manipulation functions in Python including: 1. Creating, indexing, and reversing strings. 2. Concatenating strings using + and repeating strings using *. 3. Iterating through strings to count characters, membership testing using 'in', and using built-in functions like len() to find length. 4. Additional functions covered include count(), find(), split(), startswith/endswith, replace(), changing case, and more. Examples are provided to demonstrate the output of each function.

Uploaded by

Bharat Malviya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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