String Manipulations ==================== What is a string? ================= -> a group of characters which enclosed with quotes is called as a "string". -> In python: the string can define with: single quotes double quotes triple quotes Note: ===== In other languages like java: have character data representation character ==> 'a' But, python does not support the character representation, the character representation like other languages in python considered as "string". Syntax for the string definition is: variable-name/identifier = 'group of characters' or "group of characters" ==> "str" is the class name for the strings. # string definition s1 = 'a' # characters are: alphabets, digits, special characters (,./?@! s2 = 'python' s3 = "2" s4 = "python3" print(type(s1)) print(type(s2)) print(type(s3)) print(type(s4)) s5 = ('Hi' 'Good morning.' 'Today we are learning' 'about strings.') s6 = ("Hello" "Good morning." "Today, we are going to learni" "about the strings Introduction.") print(s5) print(s6) # for multi-line string: we can use triple quotes s7 = '''Hi today, we have started about the string introduction''' s8 = """Hi Welcome to Ashok IT. We are providing various IT Skill and courses as per industry demands.""" print(s7) print(s8) s9 = "I am learnong 'python' for my 'AWS Devops' interview purpose." s10 = "I am learnong \"python\" for my \"AWS Devops\" interview purpose." # in general, we cannot write double quotes within double quotes or single quotes # within single quotes. # but we can define double quotes within single quotes or vice versa. # if you want to define the double quotes within the double quotes (nesting of quotes) # in this case, the inner quotes must be prefixed with \ print(s9) print(s10) ================================================== How to access the string characters? ==================================== -> there are two ways: 1) using indexing 2) using slicing 3) using loop statements 1) using Indexing: ================== -> Index ==> is representing with integer value Index can use to get the substring from the main string. "Python" ==> 'P', 'y', 't', 'h', 'o', 'n' (sub-strings) -> substring ==> part of string -> to acquire the character/sub-string from the main string, we can use index -> that every character in a string can be uniquely represented with a number is called as an "index". -> The indexing is define with two ways: 1) Positive indexing 2) Negative indexing -> Positive indexing can use to access the characters of the string from left to right direction. This approach is called as "Forward accessing". -> Positive indexing can start with: '0' and end with 'n-1' Here: n ==> number of characters of the string Ex: string with 10 characters: range for the positive indexing ==> 0 to 9 Syntax for string indexing is: string-name[index] Ex: s = "python" s[0] ==> 'p' s[1] ==> 'y' s[2] ==> 't' s[3] ==> 'h' s[4] ==> 'o' s[5] ==> 'n' s = "Python" print(s[0]) print(s[1]) print(s[2]) print(s[3]) print(s[4]) print(s[5]) -> Negative indexing can define with negative range of index values can use to access the characters from right to left. This kind of access is called as "Reverse accessing". -> Negative indexing can be ranged from: -1 to -n here: n ==> number of characters of string Ex: string with 10 characters: negative index range ==> -1 to -10 s = "Python" print(s[-1]) print(s[-2]) print(s[-3]) print(s[-4]) print(s[-5]) print(s[-6]) Note: ===== 1) When we define the index which is out of range, then we can get error "Index Error" Here: Index error ==> Run time error. 2) Python can understand the positive index only by default. ===================================================== Looping on strings: =================== using while loop: ================= # while loop # WAP TO ACCESS ALL THE CHARACTERS OF THE GIVEN STRING USING ITS POSITIVE AND NEGATIVE INDEX ALSO. """ HINT: s = "Python" the character at positive index '0' and negative index -6' ==> 'P' """ s = "High-level" index = 0 # initialization while index < len(s): # condition print("The Character at positive index",index,"and at negative index",index-len(s),"is = ",s[index]) index = index + 1 # update