Python_Strings


First Python Live session for Python for Beginners

Covering how to install Python, basics of setting up your environment and string/string manipulation

For debian based systems we want to run the following code

sudo apt-get install python3

Strings in Python

Python strings are "immutable" which means they cannot be changed after they are created. Since strings can't be changed, we construct new strings as we go, to represent computed values. So for example the expression ('hello' + 'there') takes in the 2 strings 'hello' and 'there' and builds a new string 'hellothere'.

Raw string r'this is a raw string'

raw strings pass all characaters to stdout and does not perform any special formatting like \n or \\


In [44]:
new_string = 'This is the first python live session'

In [48]:
new_string
new_string_ = 't' + new_string[1:]
new_string_


Out[48]:
'this is the first python live session'

Unicode strings


In [40]:
# Unicode strings
u_string = u'A unicode \u018e string \xf1'
u_string


Out[40]:
'A unicode Ǝ string ñ'

Ascii Strings

default when creating strings


In [50]:
# ascii string
a_string = 'this\tis a ascii\nstring'
print(a_string)


this	is a ascii
string

In [43]:
# r-string or raw string
r_string = r'this is an r \nstring \t'
r_string


Out[43]:
'this is an r \\nstring \\t'

In [6]:
string1 = 'hello'
string2 = 'there'
string3 = 'Mary had a little lamb'

Byte String

Working with Hex/Oct/Bin String Data

Common Erros

  • TypeError: unicode strings are not supported, please encode to bytes

We will work with split, join, rstrip, lstrip, rfind

Slice Operator


In [52]:
# to get last element
print(string1)
string1[4]


hello
Out[52]:
'o'

In [55]:
# to get the THIRD element
print(string2)
string2[2]


there
Out[55]:
'e'

In [56]:
# TO get a range of elements
print(string3)
string3[0:4]


Mary had a little lamb
Out[56]:
'Mary'

Split


In [12]:
help(str.split)


Help on method_descriptor:

split(...)
    S.split(sep=None, maxsplit=-1) -> list of strings
    
    Return a list of the words in S, using sep as the
    delimiter string.  If maxsplit is given, at most maxsplit
    splits are done. If sep is not specified or is None, any
    whitespace string is a separator and empty strings are
    removed from the result.


In [57]:
print(string3)
string3.split()


Mary had a little lamb
Out[57]:
['Mary', 'had', 'a', 'little', 'lamb']

In [58]:
le_falta_la_a = string3.split('a')
le_falta_la_a


Out[58]:
['M', 'ry h', 'd ', ' little l', 'mb']

Join


In [13]:
help(str.join)


Help on method_descriptor:

join(...)
    S.join(iterable) -> str
    
    Return a string which is the concatenation of the strings in the
    iterable.  The separator between elements is S.


In [22]:
tiene_la_a = 'a'.join(le_falta_la_a)
tiene_la_a


Out[22]:
'Mary had a little lamb'

In [1]:
string1 = ['this', 'iss', 'a', 'random', 'string']
'*'.join(string1)


Out[1]:
'this*iss*a*random*string'

rstrip


In [23]:
help(str.rstrip)


Help on method_descriptor:

rstrip(...)
    S.rstrip([chars]) -> str
    
    Return a copy of the string S with trailing whitespace removed.
    If chars is given and not None, remove characters in chars instead.


In [26]:
this_is_a_string = 'adfadfds                                    '

In [27]:
this_is_a_string.rstrip()


Out[27]:
'adfadfds'

lstrip


In [29]:
left_spaces = '             adgfgfsgsfdgsr'
left_spaces


Out[29]:
'             adgfgfsgsfdgsr'

In [30]:
left_spaces.lstrip()


Out[30]:
'adgfgfsgsfdgsr'

rfind


In [31]:
help(str.rfind)


Help on method_descriptor:

rfind(...)
    S.rfind(sub[, start[, end]]) -> int
    
    Return the highest index in S where substring sub is found,
    such that sub is contained within S[start:end].  Optional
    arguments start and end are interpreted as in slice notation.
    
    Return -1 on failure.


In [59]:
print(string3)
string3.rfind('had ')


Mary had a little lamb
Out[59]:
5

Formatting Strings

How to build strings properly.


In [66]:
'{one}{one}{one}'.format(one=5,three=3, two=4)


Out[66]:
'555'

In [62]:
stringgggg = '%d%d%d' % (5,4,3)
print(stringgggg)


543