In [1]:
import re

In [2]:
string = "li-7 1 DEN=3.327 .0784 908 end"

In [3]:
type(string)


Out[3]:
str

In [4]:
pattern = re.compile("327")

In [6]:
print(pattern.match(string))


None

In [7]:
print(pattern)


re.compile('327')

In [9]:
print(re.search(pattern, string))


<_sre.SRE_Match object; span=(13, 16), match='327'>

In [19]:
match = re.search(pattern,string)

In [11]:
match.string


Out[11]:
'li-7 1 DEN=3.327 .0784 908 end'

In [12]:
match.start()


Out[12]:
13

In [13]:
match.end()


Out[13]:
16

In [20]:
match.string[match.start():match.end()]


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-20-34141f2a7f67> in <module>()
----> 1 match.string[match.start():match.end()]

AttributeError: 'NoneType' object has no attribute 'string'

In [15]:
pattern = re.compile(r'327.*908')

In [41]:
pattern = re.compile(r'(327.*)908')

match = re.search(pattern,string)
print(string)
print(match)

match.string[match.start():match.end()]


li-7 1 DEN=3.327 .0784 908 end
<_sre.SRE_Match object; span=(13, 26), match='327 .0784 908'>
Out[41]:
'327 .0784 908'

In [27]:
print(string)
print(pattern)


li-7 1 DEN=3.327 .0784 908 end
re.compile('(327.*)908')

In [48]:
pattern = r'(327.*)908'
my_regex = r'\g<1>' + str(984)
print(my_regex)


\g<1>984

In [49]:
print(re.sub(pattern, my_regex, string))
print(re.sub(pattern, r'\1',string))


li-7 1 DEN=3.327 .0784 984 end
li-7 1 DEN=3.327 .0784  end

In [28]:
print(r'1' + str(984))


1984

In [35]:
pattern2 = re.compile(r'\\'+str(84))
string2 = r'\84'

In [36]:
print(string2)


\84

In [37]:
match = re.search(pattern2,string2)
print(match)


<_sre.SRE_Match object; span=(0, 3), match='\\84'>

In [38]:
match.string[match.start():match.end()]


Out[38]:
'\\84'

In [50]:
i = 2
if i:
    print(i)


2

In [51]:
i = 0
if i:
    print(i)

In [52]:
i = None
if i:
    print(i)

In [53]:
i = 1
if i:
    print(i)


1

In [56]:
i = "donuts"
if i:
    print(i)
elif 1 == 1:
    print("wrong syntax")


donuts

In [61]:
print('{:06.2f}'.format(3.141592653589793))
print('{:.3f}'.format(3.141592653589793))


003.14
3.142

In [ ]: