String in Python Presented by - Aryadip Dey 6 th Sem, Textile Technology University roll no. - 11001420029 Govt. College of Engineering & Textile Technology, Serampore
Content String- Introduction Reading and printing a String Accessing individual character Repetition String slicing String concatenation Splitting of string Split separator Conversion of string
String- Introduction Strings are a collection of characters which are stored together to represent arbitrary text inside a python program. We can create a string inside a python program by surrounding text with either single quotes (’), double quotes ("), or a collection of three of either types of quotes (’’’ or """). Python uses Unicode format to represent strings. Here are a few examples of how to create a string constant and assign its value to a variable: Name = ‘ aryadipdey ’ College = “ gcetts ”
Reading and printing a String Reading and printing a string in python is shown below. For example: Write a program to read and print a string print(“Enter your name”) Name = input() print(“ Hi”,Name,”Lets learn about strings in Python”)
Accessing individual character We can access individual characters of string using indexing. Indexing allows negative address references to access characters from the back of the string, e.g. -1 refers to the last character, -2 refers to the second last character and so on. (See the below figure). While accessing an index out of the range will cause an indexerror . T E X T I L E 1 2 3 4 5 6 -7 -6 -5 -4 -3 -2 -1
Repetition The asterisk (*), when used between a string and an integer creates a new string with the old string repeated by the value of the integer. The order of the arguments is not important. Example: Write a program to repeat a given string word=“hello” echo = word*5 print(echo) #output: hellohellohellohellohello
String slicing To access a range of characters in the string, method of slicing is used. Slicing in a string is done by using a slicing operator (colon). We can access substrings using slicing For example: Write a program to slice a given string C= “Whatever it takes” print(C) #output: Whatever it takes D=C[5:7] print(D) #output:ve E=C[9:11] print(E) #output: it
String concatenation Joining of two or more strings into a single one is called concatenation. The + operator does this in python. The * operator can be used to repeat the string for a given number of times. For example: Write a program to join two different string into a single one firstname = “ aryadip ” lastname = “ dey ” fullname = firstname + lastname print( fullname ) #output: aryadipdey
Splitting of string By using split command we can get a list of the words in string. For example: Write a program to create a list of word from a given string a= " i am a boy” print(a) #output: i am a boy print(type(a)) #output: <class 'str'> print( len (a)) #output: 10 w= a.split () print(w) #output: [' i ', 'am', 'a', 'boy'] print(type(w)) #output: <class 'list'> print( len (w)) #output: 4
Split separator Default separator is any space(space, newline, tab) To specify any other separator, specify it explicitly. For Example: Write a program to split a string separated with a separator a= " fibre,yarn,fabric ” print(a) #output: fibre,yarn,fabric print(type(a)) #output: <class 'str'> print( len (a)) #output: 17 w= a.split (',’) print(w) #output: ['fibre', 'yarn', 'fabric'] print(type(w)) #output: <class 'list'> print( len (w)) #output: 3
Conversion of string A string may be present in lower case or upper case. The string in lower case can be converted into upper case and vice versa using various methods of the str class.
Conversion of string (Continued)
Reference Introduction to Python Programming Course Notes by Phil Spector https://www.programiz.com/python-programming/online-compiler/ https://www.slideshare.net