Chapter 3.Working With Strings. 'Beginning Python From Novice to Professional' by Magnus Lie Hetland

String formatting operator

>>> print format % value
Hello, Pit, How do you do?
>>> format = 'Hello, %s. %s do you do?'
>>> values = ('Pit','How')
>>> print format % values
Hello, Pit. How do you do?
____________________________________
formatting real numbers (floats)

>>> format = 'Pi with four decimals: %.4f'
>>> from math import pi
>>> print format % pi
Pi with four decimals: 3.1416
_______________________________________
Template strings/Template method SUBSTITUTE

>>> from string import Template
>>> s = Template('$x, glorious $x')
>>> s.substitute(x = 'slurm')
'slurm, glorious slurm'
___________________________
If the replacement field is part of a word, x should be enclosed in braces

>>> from string import Template
>>> s = Template("It's ${x}tastic")
>>> s.substitute(x='slurm')
"It's slurmtastic"
___________________________________
If in string there is $ you should use $$
>>> s = Template('Make $$ selling $x')
>>> s.substitute(x = 'slurm')
'Make $ selling slurm'
___________________________________
Using dictionary 

>>> s = Template('A $thing must never $action')
>>> d = {}
>>> d[$thing]='Gentelman'
>>> d['thing']='Gentelman'
>>> d['action']='show his socks'
>>> s.substitute(d)
'A Gentelman must never show his socks'
___________________________________


WORKING WITH STRINGS. LONG VERSION

>>> '%s plus %s equals %s'%(1,1,2)

'1 plus 1 equals 2'
__________________________________________

String conversions

>>> 'Pi: %f' %pi
'Pi: 3.141593'
>>> 'Pi: %3f' %pi
'Pi: 3.141593'
>>> 'Pi: %.3f' %pi
'Pi: 3.142'
>>> 'Pi: %.4f' %pi
'Pi: 3.1416'
>>> 'Very inexact estimate of pi: %i' %pi
'Very inexact estimate of pi: 3'
>>> 'Using str: %s' %42L
'Using str: 42'
>>> 'Using repr: %r' %42L
'Using repr: 42L'
____________________________________________

Width & Precision

Width - min. number of characters, reserved for formatted value
Precision - number of decimals that will be included in the result or the max. number of characters the formatted value may have

>>> '%10f' %pi
'  3.141593'
>>> '%10.2f' %pi
'      3.14'
>>> '%.5f' %pi
'3.14159'
>>> '%5s' %'Guido van Rossum'
'Guido van Rossum'
>>> '%.5s' %'Guido van Rossum'
'Guido'
_____________________________________
STRING METHODS

.find()

>>> 'My back is black'.find('back')
3
>>> title = 'Monty Python flying circus'
>>> title.find('Python')
6
>>> title.find('Monty')
0
>>> title.find('Iurii')
-1
_________________________________
>>> subject = '$$$ Get rich now!!! $$$'
>>> subject.find('$$$ Get rich now!!! $$$')
0
>>> subject.find('$$$')
0
>>> subject.find('$$$',0,1)
-1
>>> subject.find('$$$',1)
20
>>> subject.find('$$$',0)
0
____________________________________

.join()

join only strings

>>> seq = [1,2,3,4,5,6]
>>> sep = '+'
>>> sep.join(seq)

Traceback (most recent call last):
  File "", line 1, in
    sep.join(seq)
TypeError: sequence item 0: expected string, int found
>>> seq = ['1','2','3','4']
>>> sep = '+'
>>> sep.join(seq)
'1+2+3+4'
>>> seq.join(sep)

Traceback (most recent call last):
  File "", line 1, in
    seq.join(sep)
AttributeError: 'list' object has no attribute 'join'
>>> dirs = '','usr','bin','env'
>>> '/'.join(dirs)
'/usr/bin/env'
_______________________________________________

.lower ()

>>> 'Moscow Big Theatre'.lower()
'moscow big theatre'
>>> if 'Gumby' in ['Gumby','ben','Xren']: print 'Found it'
...
Found it
>>> name = 'Gumby'
>>> names = ['gumby','ben','Xren']
>>> if name.lower() in names: print 'Found it'
...
Found it
______________________________________________

.title()

>>> "That's all, kids".title()
"That'S All, Kids"

alternative, more useful - capwords function from string
>>> import string
>>> string.capwords("That's all,kids")
"That's All,kids"
_______________________________________________

.replace()
>>> 'This is a test'.replace ('is','eez')
'Theez eez a test'
_______________________________________________

.split()

>>> '1+2+3+4'.split('+')
['1', '2', '3', '4']
>>> '/usr/env/egg'.split('/')
['', 'usr', 'env', 'egg']
>>> 'And does no matter'.split()
['And', 'does', 'no', 'matter']

_______________________________________________

strip.()

>>> '   good idea   '.strip()
'good idea'
>>> name = 'Gumby'
>>> names = ['Gumby','Peter','Jack']
>>> name = 'Gumby '
>>> if name.strip() in names: print 'Found it'

Found it

specifying characters are to be stripped

>>> '*** spam * is * great !!!***'.strip(' !*')
'spam * is * great'
_________________________________________________

.translate()

Translate works with single characters, but it can work with several simultaniosly

>>> from string import maketrans
>>> table = maketrans('cs','kz')
>>> len(table)
256
>>> table[97:123]
'abkdefghijklmnopqrztuvwxyz'
>>> maketrans('','')[97:123]
'abcdefghijklmnopqrstuvwxyz'
>>> 'This is an incredible test'.translate(table)
'Thiz iz an inkredible tezt'
>>> 'This is an incredible test'.translate(table,' ')
'Thizizaninkredibletezt'
________________________________________________





Popular posts from this blog

Ketamine: The Past, Present, and Potential Future of an Anesthetic Drug

Fast track anesthesia approaches