This presentation describes about the strings in python language. It also describes about the built-in string methods available in python.
Size: 585.67 KB
Language: en
Added: Jan 30, 2025
Slides: 13 pages
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: