0% found this document useful (0 votes)
28 views

Pract 7

This document contains Python code to demonstrate various string operations like concatenation, indexing, slicing, and string manipulation functions. It shows how to concatenate strings with the + operator and by taking user input. It demonstrates indexing and slicing on predefined and user input strings. It also shows the use of upper(), lower(), len(), split(), strip(), lstrip(), and rstrip() string functions.

Uploaded by

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

Pract 7

This document contains Python code to demonstrate various string operations like concatenation, indexing, slicing, and string manipulation functions. It shows how to concatenate strings with the + operator and by taking user input. It demonstrates indexing and slicing on predefined and user input strings. It also shows the use of upper(), lower(), len(), split(), strip(), lstrip(), and rstrip() string functions.

Uploaded by

Raj Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Rituraj Singh 20270106158

PRACTICAL: 7
Aim : Write Python Scripts for following:
(a) Perform concatenation on string.

(b) Perform indexing and slicing on string.

(c) Manipulate strings with following functions: lower, upper,len, split,


strip, lstrip, rstrip.

A Static

In [3]: # Adding to strings(concatenation)


s1="Rituraj "
s2="Singh" # by using (+) we can easiy add a string.
s3=s1+s2
print(s3).

Rituraj Singh

A Dynamic

In [4]: # Adding to strings(concatenation) By taking user input


s1=input('Enter string 1 = ')
s2=input('Enter string 2 = ')
s3=s1+" "+s2
print(s3)

Enter string 1 = utsav


Enter string 2 = singh
utsav singh

B Static(Indexing)

In [1]: # performing indexing on string


str = "Bhagwan Mahavir University!"
print(str[0]) # string indexing starts from 0 to
print(str[]) # to n-1th index
print(str[16])

B
r
U
B Dynamic(Indexing)

In [1]: # performing indexing on string by taking user input


str=input("Enter String = ")
a=int(input("Enter String = "))
b=int(input("Enter String = "))
c=int(input("Enter String = "))
print(str[a])
print(str[b])
print(str[c])

Enter String = Rituraj Singh


Enter String = 0
Enter String = 5
Enter String = 6
R
a
j

B Slicing

In [1]: # slicing pre-defined string


str ="Bhagwan Mahavir" # The slice() function returns a slice object.
print(str[: 3]) # Slicing done accordinf to the index number
print(str[1 : 5 : 2]) # syntax------ slice(start, end, step)
print(str[-1 : -12 : -2]) # it will not print the last index no
# where[-1] defines considering element from last

Bha
hg
rvhMnw

String Manipulation

In [8]: # check string in upper or lower case


str =input('enter string = ')
Z=(" Utsav Singh ")
A= str.upper() # upper case
B= str.lower() # lower case
C= str.strip() # Striping
D= str.lstrip() # left strip
E= str.rstrip() # right strip
F= str.split() # Spliting
print(A)
print(B)
print("My Name is", C, "I am Cr of Class Co2")
print("My Name is", D, "I am Cr of Class Co2")
print("My Name is", E, "I am Cr of Class Co2")
print(F)
print(len(str)) # length of string

enter string = UtsaV Singh


UTSAV SINGH
utsav singh
My Name is Utsav Singh I am Cr of Class Co2
My Name is Utsav Singh I am Cr of Class Co2
My Name is Utsav Singh I am Cr of Class Co2
['UtsaV', 'Singh']
11

Signature

You might also like