STRING OPERATIONS ================= WHAT IS STRING? =============== ANY TEXT GROUP OF ANY CHARACTERS WHICH ENCLOSED WITH SINGLE QUOTES OR WITH DOUBLE QUOTES IS CALLED AS "STRING" EX: 'PYTHON', "1234", 'PY@123' NOTE: ==== IF A STRING CAN START WITH SINGLE QUOTE THEN IT SHOULD END WITH SINGLE QUOTE ONLY. IF A STRING CAN START WITH DOUBLE QUOTES THEN IT SHOULD END WITH DOUBLE QUOTES ONLY. EX: 'PYTHON" OR "PYTHON' ==> INCORRECT CHARACTERS FOR DEFINING THE STRING ARE: ALPHABETS ==> LOWER CASE, UPPER CASE DIGITS ==> 0 TO 9 ANY SPECIAL CHARACTER (@,#$%^&} ASCII Vs UNICODE: ================= ASCII ==> AMERICAN STANDARD CODE INFORMATION INTERCHANGE ASCII IS A STANDARD WHICH DESCRIBE SOME RULES TO DEFINE THE INTEGER NUMBER (ASCII VALUE) FOR EACH AND EVERY CHARACTER. EX: A TO Z ==> 65 TO 90 a TO z ==> 97 TO 122 0 TO 9 ==> 48 TO 56 C/C++ ==> CHARACTER DATA ==> 1 BYTE ==> 8-BITS WE CAN REPRESENT 2^8 NUMBER OF CHARACTERS ONLY 256 CHARACTERS ==> 0 TO 255 GLOBAL LANGUAGES UNICODE: ======= PYTHON IS UNICODE DEPENDENT LANGUAGE JAVA: CHARACTER DATA ==> 2-BYTES ==> 16-BITS REPRESENT: 2^16 CHARACTERS ==> 65536 ==> 0 TO 65535 COMPUTERS ENCODE: UTF-16, UTF-32, UTF-64 ETC. NOTE: ==== IN PYTHON, THERE IS NO CHARACTER DATA REPRESENTATION. 'A' ==> STRING 'ABC' ==> STRING chr() ===== ==> is an inbuilt method, which we can use to return the character according to the UNICODE value. Syntax: chr(Unicode) ord(): ===== ==> is an inbuilt method which we can use to return a Unicode value according to the Character value. Syntax: ord(character) len(): ===== ==> is an inbuilt method which we can use to get the length of the string or any collection. Syntax: len(string-name) a = 'python' b = "Python" c = '1234' d = "abc@123" e = 'a' print(type(a),type(b),type(c),type(d),type(e)) # Displaying of All Unicodes: for i in range(65536): print("The Character at the Unicode {} is = {}".format(i,chr(i))) # Accessing of Unicodes according to the character index = 0 while index < len(a): print("The Unicode of the character {} is = {}".format(a[index],ord(a[index]))) index += 1 for k in a: print("The Unicode of character {} is = {}".format(k,ord(k))) =========================================================== String is Index Based, Slicing Supported and Ordered ===================================================== To access the individual character of the string we can use an index Index ==> an integer Note: In python, index can be with both: positive values and with negative values also. Index can represent in two ways: 1) Positive Indexing ===================== ==> It can always start with: '0' ==> It can end with: len(str)-1 ==> The Characters of the string can access from left to right. Syntax: str-name[positive index] ex: a = 'Python' a[0] ==> P a[1] ==> y a[2] ==> t a[3] ==> h a[4] ==> 0 a[5] ==> n 2) Negative Indexing ==================== ==> It can always start with: '-1' ==> It can end with: -len(str) ==> The characters of the string can access from right to left. Syntax: str-name[negative index] ex: a = "python" a[-1] ==> n a[-2] ==> o a[-3] ==> h a[-4] ==> t a[-5] ==> y a[-6] ==> p Slicing on Strings: =================== accessing the part of the string Syntax: str-name[start:stop:step] Note: ==== In slicing: start, stop and step are optional if start is not present ==> slicing can start from first character by default. if stop is not present ==> slicing can stop at last character by default if step is not present ==> difference can consider as '1' by default. a = "python" b = "Python Programming" # positive indexing print(a[0]);print(a[1]);print(a[2]);print(a[3]);print(a[4]);print(a[5]) print() # negative indexing print(a[-1]);print(a[-2]);print(a[-3]);print(a[-4]);print(a[-5]);print(a[-6]) # Slicing print(b[0:6:1]);print(b[0:6]);print(b[:6:1]);print(b[:6:]) print(b[7:len(b):1]);print(b[7::1]);print(b[7::]) # start < stop ==> for slicing # if start > stop ==> step as negative print(b[-1:-15:-1]) print(b[15:0:-1]) print(b[::]) print(b[::1]) # forward slicing print(b[::-1]) # reverse slicing # ordered print(a);print(b) ========================================= Multi-line String ================= triple quotes ==> ''' ''' or """ """ 'Python is 'High level'' ==> Incorrect ms = '''Python is High Level Programming Language''' ms1 = """Python is Object Oriented Programming Language""" ms2 = 'Python is \'High level\' programming langauge' ms3 = "Python is \"Object Oriented\" Programming Language" ms4 = 'Python is "Easy" Language' ms5 = "Python is 'Dynamically Typed' Language" print(ms);print(ms1);print(ms2);print(ms3);print(ms4);print(ms5)