In [ ]:
//To determine the length of a string//

len('hello world')

In [2]:
s= 'hello world'

In [3]:
Print s


  File "<ipython-input-3-6c3fd701e94a>", line 1
    Print s
          ^
SyntaxError: invalid syntax

In [4]:
print s


  File "<ipython-input-4-ff1a9e6c21c5>", line 1
    print s
          ^
SyntaxError: Missing parentheses in call to 'print'

In [5]:
print (s)


hello world

In [6]:
s


Out[6]:
'hello world'

In [7]:
//Indexing //

s[2]


Out[7]:
'l'

Slicing


In [8]:
S[2:]


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-b83bb2c35ff1> in <module>()
----> 1 S[2:]

NameError: name 'S' is not defined

In [9]:
s[2:]


Out[9]:
'llo world'

Grab everything upto the third index


In [10]:
s[:3]


Out[10]:
'hel'

Grab everything


In [11]:
s[:]


Out[11]:
'hello world'

Reverse a string


In [12]:
s[::-1]


Out[12]:
'dlrow olleh'

String properties

Immutability-Once a string is created ,the elements within it cannot be changed or replaced.


In [13]:
print s


  File "<ipython-input-13-ff1a9e6c21c5>", line 1
    print s
          ^
SyntaxError: Missing parentheses in call to 'print'

In [14]:
print (s)


hello world

In [15]:
s[0] = 'x'


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-d69c2f6344ea> in <module>()
----> 1 s[0] = 'x'

TypeError: 'str' object does not support item assignment

Concatenate strings


In [16]:
s + 'This is me'


Out[16]:
'hello worldThis is me'

In [17]:
s


Out[17]:
'hello world'

In [18]:
s + ' This is me!'


Out[18]:
'hello world This is me!'

In [19]:
print (s)


hello world

In [20]:
s= s + ' This is me!'

In [21]:
print(s)


hello world This is me!

Methods on strings


In [22]:
s= 'Hello'

In [23]:
s.upper


Out[23]:
<function str.upper>

In [24]:
s.upper()


Out[24]:
'HELLO'

In [25]:
s.lower()


Out[25]:
'hello'

In [26]:
s.capitalize()


Out[26]:
'Hello'

In [27]:
s.casefold()


Out[27]:
'hello'

In [28]:
s.center()


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-28-c7faacfc3b77> in <module>()
----> 1 s.center()

TypeError: center() takes at least 1 argument (0 given)

In [29]:
s.center(1)


Out[29]:
'Hello'

In [30]:
s


Out[30]:
'Hello'

In [31]:
print '%s' %(s)


  File "<ipython-input-31-3b3f9a439858>", line 1
    print '%s' %(s)
             ^
SyntaxError: invalid syntax

In [34]:
x= 'Hello'


  File "<ipython-input-34-cef81a1e2987>", line 3
    print 'Place : %s' %(x)
                     ^
SyntaxError: invalid syntax

In [35]:
print 'Place my varaible here : %s' %(x)


  File "<ipython-input-35-505e5b67b792>", line 1
    print 'Place : %s' %(x)
                     ^
SyntaxError: invalid syntax

In [37]:
print ('Place my varaible here : %s' %(x))


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-37-f10f006486e7> in <module>()
----> 1 print ('Place my varaible here : %s' %(x))

NameError: name 'x' is not defined

In [38]:
x='Hello'
print ('Place my varaible here : %s' %(x))


Place my varaible here : Hello

In [39]:
x='hello'
print ('%s' %(x))


hello

In [ ]: