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

PP Exp-3docx

Uploaded by

DheerajKhade
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)
13 views5 pages

PP Exp-3docx

Uploaded by

DheerajKhade
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/ 5

EXPERIMENT - 3

Name :Chaaitanya Deshmukg Roll No : 60


Subject : Python Programming ( PP ) Class / Batch :B3

Date of Performance : Date of Submission :

Aim

Write a python program to create , concatenate and print a string and accessing sub
– string from a given string .

Theory

In Python , a string is a sequence of characters enclosed within either single quotes


( ' ) , double quotes ( " ) , or triple quotes ( ' ' ' or " " " ) . It's an immutable data type ,
meaning once created , its value cannot be changed .
Creating Strings --

# Single quotes
string1 = ' Hello , World ! '

# Double quotes
string2 = " Python is fun "

# Triple quotes for multiline strings


string3 = " " " This is a multiline string " " "
Accessing Characters -- You can access individual characters using indexing ( starts from 0 ) :

string = " Python "


print ( string[0] ) # Output : P
print ( string[2] ) # Output : t
print ( string[-1] ) # Output : n

Slicing Strings -- You can extract a substring using slicing :

print ( string[2:5] ) # Output : thon

String Concatenation -- Combining strings

greeting = " Hello "


name = " Alice "
message = greeting + " , " + name + " ! "
print ( message ) # Output : Hello , Alice !

String Length -- Finding the length of a string :

length = len ( string )


print ( length ) # Output : 6

String Methods -- Python offers numerous built – in methods for string manipulation :

text = " This is a sample string "


# Uppercase
print ( text.upper() ) # Output : THIS IS A SAMPLE STRING
# Lowercase
print ( text.lower() ) # Output : this is a sample string
# Strip leading and trailing whitespace
print( text.strip() ) # Output : This is a sample string
# Replace a substring
print ( text.replace ( " sample " , " example " ) ) # Output : This is an example string
# Find the index of a substring
print ( text.find ( " is " ) ) # Output : 3
# Check if a string starts with a specific substring
print ( text.startswith ( " This " ) ) # Output : True
# Check if a string ends with a specific substring
Print ( text.endswith ( " string " ) ) # Output : True
# Split a string into a list
words = text.split()
print ( words ) # Output : [ ' This ' , ' is ' , ' a ' , ' sample ' , ' string ' ]
TYPES OF STRINGS -

Python primarily has one primary string type : str . This represents
text data and is the most commonly used string type .

How Strings are Created

While there's only one primary type , there are different ways to
create strings :

1. Single Quotes :
string1 = ' This is a string '

2. Double Quotes :
string2 = " This is also a string "

3. Triple Quotes : Used for multi – line strings :


multiline_string = " " " This is a multiline string " " "
Key Points to Remember :

 Immutability : Strings are immutable , meaning their content


cannot be changed after creation .
 Indexing and Slicing : You can access individual characters or
substrings using indexing and slicing .
 String Methods : Python provides a rich set of methods for
string manipulation ( e.g., upper() , lower() , strip() , split() , etc. )
.
 String Formatting : You can format strings using f - strings or
the format() method .
1)Code_1
Input:

Output:
2)Code_2

Input:

Output:

You might also like