In [ ]:
//To determine the length of a string//
len('hello world')
In [2]:
s= 'hello world'
In [3]:
Print s
In [4]:
print s
In [5]:
print (s)
In [6]:
s
Out[6]:
In [7]:
//Indexing //
s[2]
Out[7]:
Slicing
In [8]:
S[2:]
In [9]:
s[2:]
Out[9]:
Grab everything upto the third index
In [10]:
s[:3]
Out[10]:
Grab everything
In [11]:
s[:]
Out[11]:
Reverse a string
In [12]:
s[::-1]
Out[12]:
String properties
Immutability-Once a string is created ,the elements within it cannot be changed or replaced.
In [13]:
print s
In [14]:
print (s)
In [15]:
s[0] = 'x'
Concatenate strings
In [16]:
s + 'This is me'
Out[16]:
In [17]:
s
Out[17]:
In [18]:
s + ' This is me!'
Out[18]:
In [19]:
print (s)
In [20]:
s= s + ' This is me!'
In [21]:
print(s)
Methods on strings
In [22]:
s= 'Hello'
In [23]:
s.upper
Out[23]:
In [24]:
s.upper()
Out[24]:
In [25]:
s.lower()
Out[25]:
In [26]:
s.capitalize()
Out[26]:
In [27]:
s.casefold()
Out[27]:
In [28]:
s.center()
In [29]:
s.center(1)
Out[29]:
In [30]:
s
Out[30]:
In [31]:
print '%s' %(s)
In [34]:
x= 'Hello'
In [35]:
print 'Place my varaible here : %s' %(x)
In [37]:
print ('Place my varaible here : %s' %(x))
In [38]:
x='Hello'
print ('Place my varaible here : %s' %(x))
In [39]:
x='hello'
print ('%s' %(x))
In [ ]: