5.1. String#
Immutable
5.1.1. List to string#
‘separator’.join(iterable)
takes list as input
returns string which is concat of these list elements
''.join(['Sahil' 'Choudhary'])
'SahilChoudhary'
','.join(['A','B','C','D'])
'A,B,C,D'
5.1.2. String to List#
str_var.split(‘-‘)
Takes String
Returns List
'sahil-choudhary'.split('-')
['sahil', 'choudhary']
5.1.3. Iterating over Strings#
# For iterations,for loop is not pythonic way,using comprehensions is pythonic way
s='sahil choudhary'
[x for x in s]
['s', 'a', 'h', 'i', 'l', ' ', 'c', 'h', 'o', 'u', 'd', 'h', 'a', 'r', 'y']
5.1.4. Reversing a String#
'sahil'[::-1]
'lihas'
5.1.5. Note#
input() always takes input as string
convert it to int/float as required