String Manipulations ===================== What is String? =============== -> It is a character or group of characters must be enclosed with: either single quotes ('') or double quotes ("") or triple quotes (''' '''/""" """) Note: ==== Like other high level programming languages, Python does not have character datatype. -> The character data also considered as "string" in python # string definition str1 = 'a' str2 = 'abc' str3 = "b" str4 = "bcde" str5 = '''a''' str6 = '''abcdef''' str7 = """p""" str8 = """Pqrst""" print(type(str1),type(str2),type(str3)) print(type(str4),type(str5),type(str6)) print(type(str7),type(str8)) Q: Is it possible to define the multi-line string? ================================================== Ans: yes # Multi-line string # multi-line string can always define with triple quotes mlst = '''Hello Good Evening. Today, we are discussing about Strings with multiple built-in functions. ''' print(type(mlst)) print(mlst) mlst1 = """ HI This is Ravi from Ashok IT Teaching about Python fullstack and Java fullstack. """ print(mlst1) mlst2 = ('Python Fullstack' 'which includes Core Python, Advanced Python, Frameworks, ' 'Libraries') print(mlst2) Q: Is it possible to place double quotes within double quotes or vice versa. No. a = "Python \"Full stack\" Development" b = 'Python \'Full stack\' Development' c = "Python 'full stack' development" d = 'Java "full stack" development' print(a) print(b) print(c) print(d) =================================================================== Accessing of Characters of the string: ====================================== 1) Using Index ============== -> Index ==> a number (integer) Note: ===== Python supports positive indexing and negative indexing also. Syntax: string-data-name[index] Positive Indexing ================= -> In python, positive indexing can use to do the forward accessing (left to right) on the strings to get individual characters of the string. -> Positive indexing can always start with '0' and end with 'l (total number of characters) - 1' Ex: s = "Python" here: range of positive index values are ==> 0 to 5 s[0] ==> 'P' s[1] ==> 'y' s[2] ==> 't' s[3] ==> 'h' s[4] ==> 'o' s[5] ==> 'n' Negative Indexing: ================== -> Negative Indexing can use to access the characters from right to left (reverse access). -> Range of negative index ==> '-1 to - l (number of characters of the string)' s[-1] ==> 'n' s[-2] ==> 'o' s[-3] ==> 'h' s[-4] ==> 't' s[-5] ==> 'y' s[-6] ==> 'P' # indexing s = "Python" # positive indexing print(s[0]) print(s[1]) print(s[2]) print(s[3]) print(s[4]) print(s[5]) print(s[-1]) print(s[-2]) print(s[-3]) print(s[-4]) print(s[-5]) print(s[-6]) 2) Using Slice operator ======================= -> Slicing is defined as acquiring of the part of the string. Ex: "Python Programming Language" op ==> "Python" op1 ==> "Program" op2 ==> "Language" Syntax: string-name[start:stop:step] Here: start ==> start index (starting point for slicing) stop ==> stop index (stopping point for slicing) step ==> difference Note: ===== 1) If you want to do the slicing from first character (index 0) of the given string: Syntax: string[0:stop:step] or string[:stop:step] 2) stop value can always consider the '-1' from the given value. 3) default value for step ==> '1' 4) When you want to do the slicing till the last character: Syntax: string[start: len(string)] or string[start:] s = "Python Programming Language" print(s[0:5]) print(s[0:6]) print(s[:6]) print(s[:6:1]) print(s[:6:]) print(s[19:len(s)]) print(s[19:]) print(s[0:len(s):3]) print(s[::3]) print(s[::]) print(s[::-1]) 3) Using loop statements ======================== While loop: ========== s = "Object Oriented Programming Language" print("The Total number of characters of the string = ",len(s)) # here: the positive index range ==> 0 to 35 # negative index range ==> -1 to -36 # forward looping i = 0 while i < len(s): print(s[i],end = " ") i += 1 print() i = -1 while i >= -len(s): print(s[i],end = " ") i -= 1 ========================================================= 4) Using built-in functions