Strings Strings • We have talked about string as sequence of characters • Strings are indicated by ' ' or " ". • We have non printed characters: ● new line '\n' ● tab '\t' String Representation ord(): takes a character and returns its Unicode integer value. chr():takes an integer and returns the Unicode character String Index • As the elements in string are stored in sequence , each element is associated with an index (location in sequence). • First element in sequence from the left is at index 0 and the last element in index (size-1) • Negative values count down from the right, starting with index -1. my_str='Hello CS101' print(my_str[0]) print(my_str[-1]) print(my_str[-3]) print(my_str[-12]) Output: H 1 1 IndexError: string index out of range String Slicing • Strings are a sequence type, having characters ordered by position from left to right. • An individual character is read using brackets. str='abcd' print('str[1]=',str[1]) Output: str[1]= b String Slicing • A programmer often needs to read more than one character at a time. • We can use slicing notation: – str[start:end] ->which creates a new string whose value mirrors the characters of str from positions start to end – 1. – my_str='abc def' – print('my_str[1:5]=',my_str[1:5]) – Output: – my_str[1:5]= bc d String Slicing my_str='abc def' print('my_str[:-1]=',my_str[:-1]) print('my_str[3:-1]=',my_str[3:-1]) print('my_str[0:-2]=',my_str[0:-2]) Output: my_str[:-1]= abc de my_str[3:-1]= de my_str[0:-2]= abc d Stride • The stride determines how much to increment the index after reading each element. numbers = '0123456789' print('numbers[::]: %s' % numbers[::]) print('numbers[::2]: %s' % numbers[::2]) print('numbers[::0]: %s' % numbers[::0]) print('numbers[1:9:3]: %s' % numbers[1:9:3]) Output: numbers[::]: 0123456789 numbers[::2]: 02468 Slicing can not be 0 numbers[1:9:3]: 147 Copy Of String • How to make a copy of a string: str1='Hi Alex' str2=str1[:] print(str2) Output: Hi Alex • How to reverse a string: str1='Hi Alex' rev=str1[::-1] print(rev) Output: xelA iH Strings Example: str1='Hi Alex' rev=str1[::2] print(rev) Output: H lx rev=str1[::-2] print(rev) Output: xl H Copy and Assignment Operator • id(): Tells us the memory address of a variable. Memory address of S • What will happen: (e.g. 56547592) s="Hello CS101" s1=s Hello CS101 S1 S print("s1 is %s"%s1) print("Location of s is",id(s),"and location of s1 is",id(s1)) Output: s1 is Hello CS101 Location of s is 56547592 and location of s1 is 56547592 Copy and Assignment Operator Memory address of S (e.g. 56547592) s="Hello CS101" Hello CS101 S S2 s2=s[:] print("s2 is %s"%s2) print("Location of s is",id(s),"and location of s2 is",id(s2)) Output: s2 is Hello CS101 Location of s is 56547592 and location of s2 is 56547592 Note: Since strings are immutable, there is not a distinction between copy and assignment. We can't change a string, we can only assign a variable to be a new string value. Copy and Assignment Operator s="Hello CS101" s1=s print("s1 is %s"%s1) print("Location of s is",id(s),"and location of s1 is",id(s1)) s2=s[:] print("s2 is %s"%s2) print("Location of s is",id(s),"and location of s2 is",id(s2)) s="Hello again" print(s,"and Location of s is",id(s)) print(s1,"and location of s1 is",id(s1)) print(s2,"and location of s2 is",id(s2)) Output: s1 is Hello Location of s2 is Hello Location of Hello again Hello CS101 Hello CS101 CS101 s is 56547592 and location of s1 is 56547592 CS101 s is 56547592 and location of s2 is 56547592 and Location of s is 59200960 and location of s1 is 56547592 and location of s2 is 56547592