String and MathFunc
String and MathFunc
UA.DETI.IP
Carlos Costa
Summary
Strings
– Review
– Special characters
– Slicing
– Content check
– Modifications
– Methods
– Formatting
Mathematical modules
– module concept
– math and cmath modules
2
Python String (review)
A sequence of characters
>>> print("IP course 2021-22") #OK
>>> print('IP course 2021-22') #OK
>>> age = 20
>>> print (full_name + " age: " + str(age))
Possible to convert numbers >>> 100 + int(”33”)
3
Special characters
The backslash (\) is used to introduce a special character
S = "Aveiro city!"
S[0:6] A v e i r o c i t y !
S[7:] A v e i r o c i t y !
S[-8:-6] A v e i r o c i t y !
S[0:5:2] A v e i r o c i t y !
7
* stop: end position not included
String slicing (cont)
>>> S = "Aveiro city!"
>>> S[0:6] # default step - 1
'Aveiro'
>>> S[7:] # default end position - str length
'city!'
>>> S[-8:-6] # negative start | end positions
'ro'
>>> S[0:5:2] # defined step - 2
'Aer'
>>> S[:6] # default start position - [0]
'Aveiro'
>>> S[:6:-1] # negative step – start at the end
'!ytic' # of the string and move left
>>> S[::-1] # reverse the string
'!ytic orievA'
>>> S[7:-2:] # combine positive and neg indexes
'cit'
8
Check string content
in keyword
– check if a certain phrase or character is present in a string
– returns True or False
– can be used in a if statement (we will see later)
9
String immutability and modifications
Syntax
<object>.<method>(<parameters>)
Examples
>>> txt.upper()
'HELLO STUDENTS!'
11
String methods (cont)
12
String formating
2. f-strings (f”…”)
>>> s = f" The bill value is ${total:0.2f}"
>>> print (s)
The bill value is $1.50
13
str.format() method
14
str.format() method (cont)
Example
>>> print('{quantity} {item} cost
${price}'.format(quantity=6, item='bananas', price=1.74))
6 bananas cost $1.74
15
str.format() method examples
>>> x, y, z = 1, 2, 3
>>> '{0}, {1}, {baz}'.format(x, y, baz=z)
'1, 2, 3’
# formatting...
>>> '{} {}'.format('one', 'two')
'one two'
>>> '{:*>10}'.format('test')
'******test'
>>> '{:04d}'.format(42)
'0042'
>>> '{:5.2f}'.format(3.2451)
' 3.25'
>>> print(1,2,3)
1 2 3
>>> print('{:4d}{:4d}{:4d}'.format(1,2,3))
1 2 3
>>> print(10,20,30)
10 20 30
>>> print('{:4d}{:4d}{:4d}'.format(10,20,30))
10 20 30
16
f-String
Example
>>> name = "Sofia"
>>> age = 33
>>> f"Hello, {name}. You are {age}."
'Hello, Sofia. You are 33.'
17
Examples: Str-method versus f-String
Str-method F-String
# 'one two'
'{} {}'.format('one', 'two') f"{'one'} {'two'} "
# '******test'
'{:*>10}'.format('test’) f"{'test':*>10}”
# '0042'
'{:04d}'.format(42) f'{42:04d}'
# ' 3.25'
'{:5.2f}'.format(3.2451) f'{3.2451:5.2f}'
# 1 2 3
'{:4d}{:4d}{:4d}'.format(1,2,3) f'{1:4d}{2:4d}{3:4d}'
# 10 20 30
'{:4d}{:4d}{:4d}'.format(10,20,30) f'{10:4d}{20:4d}{30:4d}'
18
Math functions
Python Module?
– File defining a collection of related functions and objects
>>> cmath.sqrt(-1)
1j
20
Some math functions
math. Description
pi An approximation of pi.
e An approximation of e.
sqrt(x) The square root of x.
sin(x) The sine of x.
cos(x) The cosine of x.
tan(x) The tangent of x.
asin(x) The inverse of sine x.
acos(x) The inverse of cosine x.
atan(x) The inverse of tangent x.
log(x) The natural (base e) logarithm of x.
log10(x) The common (base 10) logarithm of x.
exp(x) The exponential of x.
ceil(x) The smallest whole number>= x.
floor(x) The largest whole number <= x.
degrees(x) Convert angle x from radians to degrees.
radians(x) Convert angle x from degrees to radians.
help(math) Shows all math functions in the console
21