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

Class 8(Strings)

The document provides an overview of string operations in Python, including string creation, concatenation, repetition, indexing, and methods like startswith(), split(), and replace(). It highlights that strings are immutable and cannot be modified in place. Examples are given to illustrate various string manipulations and methods.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Class 8(Strings)

The document provides an overview of string operations in Python, including string creation, concatenation, repetition, indexing, and methods like startswith(), split(), and replace(). It highlights that strings are immutable and cannot be modified in place. Examples are given to illustrate various string manipulations and methods.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

17rydzxd0

December 16, 2024

A string is a:
� Sequence of characters
� Sequence of “pure” unicode characters (there is no specific encoding like UTF-8)

[1]: str1="Hello world"


str2='new year'
str3=""" welcome """
str4=''' hi to all '''

[2]: print(str1)
print(str2)
print(str3)
print(str4)

Hello world
new year
welcome
hi to all
Concatenation
Strings can be glued together (concatenated) with the + operator.

[4]: str5="Hello"
str6="World"
print(str5+str6)

HelloWorld

[6]: "New"+" Year"

[6]: 'New Year'

[8]: "Happy"+' to'+""" see """+''' you '''

[8]: 'Happy to see you '

Repetition
Strings can be repeated or repeatedly concatenated with the asterisk operator ”*”.

1
[12]: print(str5*3)

HelloHelloHello

[13]: print("Repitition"*4)

RepititionRepititionRepititionRepitition
Indexing
A string can be indexed using index() method.

[14]: str7="Welcome to String Operations"


print(str7)

Welcome to String Operations

[15]: print(str7.index("c"))

[16]: print(str7.index("S"))
print(str7.index("n"))
print(str7.index("O"))

11
15
18
A string in Python:
� Consists of a series of characters such as letters, numbers, and special characters
� Can be subscripted or indexed (Similar to C, the first character of a string in Python has the
index 0.)

[17]: str8="Subscripted Variable"


print(str8)

Subscripted Variable

[24]: print(str8[0])
print(str8[len(str8)-1])
print(str8[-2])
print(str8[6])

S
e
l
i

2
[26]: print("welcome"[2:4])
print("hello to all"[4:8])

lc
o to

[30]: str9="string using step size"


print(str9[2:10:2])
print(str9[-2:-10:-1])

rn s
zis pets

[32]: str10="upper case"


str11="LOWER CASE"
print(str10.upper())
print(str11.lower())

UPPER CASE
lower case
The startswith() method returns True if a string starts with the specified prefix(string). Else, it
returns False.
[34]: str12="string starts with demo"
print(str12.startswith("string"))
print(str12.endswith("hello"))

True
False
The split() method breaks up a string at the specified separator and returns a list of strings.

[40]: str13="String with space separator"


str14="String,with,comma,separator"
str13.split(" ")

[40]: ['String', 'with', 'space', 'separator']

[41]: str14.split(",")

[41]: ['String', 'with', 'comma', 'separator']

Python strings cannot be changed as change in indexed position will raise an error.
[42]: str14="strings can't be modified"
print(str14)

strings can't be modified

3
[43]: str14[10]='a'

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-43-4b9801e2c888> in <cell line: 1>()
----> 1 str14[10]='a'

TypeError: 'str' object does not support item assignment

[44]: str15="this is replace demo"


print(str15)

this is replace demo

[45]: str16=str15.replace("demo","operation")
print(str16)

this is replace operation

[46]: First_Name="string operations"


print(First_Name.capitalize())

String operations

[48]: strip_string=" welcome to strip operations "

[49]: strip_string.strip()

[49]: 'welcome to strip operations'

[50]: strip_string.lstrip()

[50]: 'welcome to strip operations '

[51]: strip_string.rstrip()

[51]: ' welcome to strip operations'

You might also like