Python Strings & Built-in String Methods.pptx

Rajapriya82 31 views 13 slides Jan 30, 2025
Slide 1
Slide 1 of 13
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13

About This Presentation

This presentation describes about the strings in python language. It also describes about the built-in string methods available in python.


Slide Content

Python Strings Mrs.S.RAJAPRIYA , M.S(IT)., Assistant Professor, Department of Computer Science (self) V.V.Vanniaperumal College for Women, Virudhunagar

Python Strings Python treats strings as contiguous series of characters delimited by single, double or even triple quotes . We can declare and define a string by creating a variable of string type. This can be done in several ways which are as follows: name = "India" graduate = 'N‘ country = name nationality = str ("Indian") Built-in String Class

Three Main Operations in string Concatenating: We can concatenate two strings using + operator. Example: s1=“Welcome to “ s2=“Python Programming” s3=s1+s2 print(s3) Output: Welcome to Python Programming

Three Main Operations in string Appending: We can add one string to the end of another string using append operator +=. Example: s1=“Hello,“ name=input(“Enter Your Name:”) s1 += name s1 += “.Welcome to the World of Python” print(s1) Output: Enter Your Name: Arun Hello, Arun .Welcome to the World of Python

Three Main Operations in string Repeating: We can repeat a given string n number of times using * operator . Example: s1=“Hello World“ print(s1*3) Output: Hello World Hello World Hello World

Str () function The str () function is used to convert values of any data type to string type. Example: s1=“Hello” s1=“Hello” n=7 n=7 s2=s1+str(n) s2=s1+n print(s2) print(s2) Output : Hello7 Output: TypeError

Strings are Immutable Python strings are immutable which means that once created they cannot be changed . Whenever you try to modify an existing string variable, a new string is created. Using id() function, we can identify the memory address of a particular object. Example: Program to demonstrate string references using id() function s1 = “Hello” Output print(s1) Hello print(id(s1)) 45093344 s2 = “World” World print(s2) 45093312 print(id(s2)) s1 += s2 print(s1) Hello World print(id(s1)) 43861792

Built-in String Methods Python support various built-in methods to manipulate strings. Each method is invoked or calle d on an object. Some of the commonly used built-in methods are:

Built-in String Methods

Built-in String Methods

Thank You