STRING MANIPULATION By Himanshi dixit 11 th B

STRING MANIPULATION ØBy Himanshi dixit 11 th ’B’

INTRODUCTION ØStrings are sequence of characters. Ø Strings are enclosed in quotes of any type. Ex: - ‘string’, “string” and ‘‘‘string’’’ ØStrings are immutable. ØAn empty string is a string that has 0 characters. Ex: - “” ØEach character of string has a unique position-id /index.

INDEX ØThe indexes of a string begin from 0 to (length-1) in forward direction and -length in backward direction -6 -1 P 0 5 -5 -4 Y T 1 2 -3 H -2 O 3 N 4

TRAVERSING A STRING ØTraversing refers to iterating through the elements of a string one character at a time. ØIndividual characters of a string are accessible through the unique index of each character. Example: name=‘superb’ for i in name : print (i, ‘-’, end=“ “) -This loop will traverse through string name The code will print: s-u-p-e-r-b- character by character.

String operators Basic operators ØThe two basic operators of strings are : + and *. String concatenation operator + ØThe + operator creates a new string by Example s Expression Will result s into ‘ 1’+’ 1’ ‘ 11’ “a”+” 0” “a 0” ‘ 123’+’abc’ ‘ 123 abc’ joining the two operand strings, e. g. , “tea”+ “pot “ Will result into“teapot” NOTE : The + oprator has to have both operands of the same type either of number types or of string types.

STRING REPLICATION OPERATOR * Ø* Operator performs replication rather than multiplication in strings. ØFor replication operator , python creates a new string that is a number of repetitions of the input string. Eg : - 3* “go! ” will return “go!go!go! ” ØThe * operator has to either have both operands of the number types (for multiplication)or one string type and one number type (for replication). It cannot work with both operands of string types.

Working of python * operator Operands data type Numbers Operation performed by * Example Multiplication 9 * 9=18 String , Number Replication ‘#’ * 3= ‘###’ Number , String Replication 3 * ’#’=‘###’

MEMBERSHIP OPERATORS ØThere are two membership operators for strings i. e. in and not in. ØMembership operators (when used with strings) , require both operands used with them are of string type, i. e. <string> in <string> not in <string> Eg: “ 12” in “xyz” “ 12” not in “xyz”

COMPARISON OPERATORS ØPython standard comparison operators i. e. , all relational operators (<, <=, >, >=, ==, !=) apply to strings also. Eg: “a” == “a” “A” != “a” will give true “ABC” == “abc” will give false ØFor comparisons like less than (<) or greater than (>) , common characters and their ordinal values should be known.

Common characters and their ordinal values Characters ‘ 0’ to ‘ 9’ Ordinal Values 48 to 57 ‘A’ to ‘Z’ 65 to 90 ‘a’ to ‘z’ 97 to 122 ‘a’ < ‘A’ will give false ‘abc’ <= ‘ABCD’ will give false

Determining ordinal value of single character ØBuilt -in function ord() that takes a single character and returns the corresponding ordinal Unicode value. ord ( <single -character>) Eg: ord ( “A”) 65 ØThe opposite of ord() function is chr() ØThe chr() takes the ordinal value in integer form and returns the character corresponding to that ordinal value. chr(<int>)

- Slides: 12