String Methods

In [1]:
name = 'Benedict Cumberbatch'
In [2]:
name.upper()
Out[2]:
'BENEDICT CUMBERBATCH'
In [3]:
name
Out[3]:
'Benedict Cumberbatch'

It doesn't change the actual value.

To change the string value stored in a variable, we do the following

In [4]:
name = name.upper()
name
Out[4]:
'BENEDICT CUMBERBATCH'
In [5]:
name.lower()
Out[5]:
'benedict cumberbatch'

isupper or islower

In [6]:
name.isupper()
Out[6]:
True
In [7]:
somestr = 'ThIs iS A StRiNg'
somestr.isupper()
Out[7]:
False
In [8]:
somestr.islower()
Out[8]:
False
In [9]:
nothing = ''
In [10]:
nothing.isupper()
Out[10]:
False
In [11]:
nothing.islower()
Out[11]:
False
In [12]:
'12345'.isupper()
Out[12]:
False
In [13]:
'something here'.islower()
Out[13]:
True
In [14]:
'elon Musk'.upper()
Out[14]:
'ELON MUSK'
In [15]:
'elon Musk'.upper().isupper()
Out[15]:
True




isalpha() = letters only
isalnum = letters and numbers only
isdecimal = numbers only
isspace() = whitespace only
istitle = titlecase only

In [16]:
'hello'.isalpha()
Out[16]:
True
In [17]:
'hello123'.isalpha()
Out[17]:
False
In [18]:
'123'.isdecimal()
Out[18]:
True
In [19]:
'     '.isspace()
Out[19]:
True
In [20]:
'This Is A Example Of Title Case'.istitle()  #All words begin with uppercase
Out[20]:
True
In [21]:
'i am gonna make this title case'.title()
Out[21]:
'I Am Gonna Make This Title Case'
In [22]:
'Elon Musk'[4].isspace()
Out[22]:
True




startswith()
endswith()

In [23]:
'Iron Man'.startswith('Iron')
Out[23]:
True
In [24]:
'Iron Man'.startswith('')
Out[24]:
True
In [25]:
'Tony Stark!'.endswith('Stark!')
Out[25]:
True
In [26]:
'Tomy Stark!'.endswith('Stark')
Out[26]:
False




join()

joins list of strings with whatever character we want to have between them of lack of it thereof.

In [27]:
','.join(['some','things','here'])
Out[27]:
'some,things,here'
In [28]:
'00000000'.join(['some','things','here'])
Out[28]:
'some00000000things00000000here'
In [29]:
'\n\n'.join(['some','things','here'])
Out[29]:
'some\n\nthings\n\nhere'
In [30]:
print('\n\n'.join(['some','things','here']))
some

things

here
In [31]:
''.join(['some','things','here'])
Out[31]:
'somethingshere'




split()

Called on a string value and returns a list of strings. By default it spilts on whitespace characters

In [32]:
'This is captain Kirk speaking'.split()
Out[32]:
['This', 'is', 'captain', 'Kirk', 'speaking']
In [33]:
'This is captain Kirk speaking'.split('i')
Out[33]:
['Th', 's ', 's capta', 'n K', 'rk speak', 'ng']
In [34]:
'55055502202'.split('0')
Out[34]:
['55', '555', '22', '2']

How to add these numbers

In [35]:
addlist = '55055502202'.split('0')
In [36]:
total = 0
for i in addlist:
    total = total + int(i)

print(total)

634




ljust()
rjust

ljust and rjust will return a padded verion of the string they are called on with spaces inserted to left justify or right justify the text

In [37]:
'Hello'.rjust(10)
Out[37]:
'     Hello'

What it does:

In [38]:
len('Hello'.rjust(10))
Out[38]:
10
In [39]:
'Hello'.rjust(4)
Out[39]:
'Hello'
In [40]:
'Hello'.ljust(20)
Out[40]:
'Hello               '

Optional second argument can be specified to specify what should be used to fill.

In [41]:
'Emma'.ljust(20,'*')
Out[41]:
'Emma****************'




center()

works just like ljust and rjust but centers the original string

In [42]:
'Hello'.center(20,'*')
Out[42]:
'*******Hello********'
In [43]:
len('*******')         #length of the asterisks added in the left side
Out[43]:
7
In [44]:
len('********')        #Length of the asterisks added in the right side
Out[44]:
8
In [45]:
len('Hello'.center(20,'*'))
Out[45]:
20




strip()
lstrip()
rstrip()

Used to remove excess character from the both ends of string, left side of the string, right side of the string for strip(), lstrip(), rstrip() respectively. By default, the characters we want to remove is set to whitespace

In [46]:
somestr = '       hello      '
In [47]:
somestr.strip()
Out[47]:
'hello'
In [48]:
somestr        #Doesn't change the original string variable
Out[48]:
'       hello      '
In [49]:
somestr.lstrip()
Out[49]:
'hello      '
In [50]:
somestr.rstrip()
Out[50]:
'       hello'

We can specify to remove any character(s) we want.

In [51]:
somestr = 'SpamSpamBaconEggsSpamEggsSpamSpam'


order of characters in the argument of strip doesnt't matter. It's going to remove all of them. In the following case - S,p,a,m from the ends

In [52]:
somestr.strip('maSp')
Out[52]:
'BaconEggsSpamEggs'

Notice the Spam in the inside isn't removed.
i.e when it encounters some character it isn't supposed to remove, it stops.




replace()

In [53]:
spam = 'street'
In [54]:
spam.replace('e', 'XYZ')
Out[54]:
'strXYZXYZt'
In [55]:
spam
Out[55]:
'street'
In [56]:
spam = spam.replace('e', 'XYZ')
In [57]:
spam
Out[57]:
'strXYZXYZt'




pyperclip module

copy()
paste()

copy and paste functions from pyperclip module are used to copy to the clipboard or paste from clipboard respectively.

In [58]:
import pyperclip

copy('hello')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-58-3a578149dca0> in <module>()
      1 import pyperclip
      2
----> 3 copy('hello')

NameError: name 'copy' is not defined

I will leave that error for future reference

In [59]:
import pyperclip

pyperclip.copy('hello!!!!!!')
In [60]:
pyperclip.paste()
Out[60]:
'hello!!!!!!'
In [61]:
something = pyperclip.paste()
In [62]:
something
Out[62]:
'hello!!!!!!'