0% found this document useful (0 votes)
10 views

Strings Math

This document provides an introduction and overview of strings, formatted output, and basic math functions in Python. It discusses how to work with strings, including accessing characters, concatenation, formatting, and methods. It also covers using the str.format() and f-string methods for variable substitutions and formatting in strings. Finally, it reviews importing and using functions from the math and cmath modules, like sqrt, sin, log, and other common mathematical operations.

Uploaded by

Salvador Modesto
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Strings Math

This document provides an introduction and overview of strings, formatted output, and basic math functions in Python. It discusses how to work with strings, including accessing characters, concatenation, formatting, and methods. It also covers using the str.format() and f-string methods for variable substitutions and formatting in strings. Finally, it reviews importing and using functions from the math and cmath modules, like sqrt, sin, log, and other common mathematical operations.

Uploaded by

Salvador Modesto
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Introdução à Programação

TP 02
S TRINGS , F ORMATTED O UTPUT, M ATH F UNCTIONS

B a s e a d o n o s M a te r i a i s d e F u n d a m e n to s d e P ro g ra m a ç ã o d e
J o ã o M a n u e l Ro d r i g u e s
António J. R. Neves

1
Topics
• Strings
• Formatted output
• Basic functions from library
• Modules math, cmath

2
Strings (1)
• Strings are sequences of characters.
• String literals are delimited by single or double quotes.
fruit = 'orange'

• Function gives the number of characters


len(fruit) #-> 6

• We can access one or a group of characters by it’s


position (the first positions has index 0)
fruit[1] #-> 'r'
fruit[3:5] #-> 'ng'

• We can also concatenate and repeat strings.


name = 'tom' + 'cat' #-> 'tomcat'
gps = 2 * 'tom' #-> 'tomtom'

3
Strings (2)

• Strings are immutable. Methods that imply modification


actually only return a new string object.
fruit.upper() #-> 'ORANGE'
fruit.replace('a', 'A') #-> 'orAnge'
fruit #-> 'orange' (not changed)

• Characters (letters, digits, punctuation) are stored as


numeric codes (according to Unicode in Python3).
– ord(c) - returns the code of the character c.
– chr(n) - returns the character represented by code n.

• The str class has various built-in methods for checking


for different classes of characters: see all with help(str)

4
String methods

• Strings have a lot of useful methods. Play ▶

str.isalpha() True if all characters are alphabetic.


str.isdigit() True if all characters are digits.
str.is... ...
str.upper() Convert to uppercase.
str.lower() Convert to lowercase.
... ...
str.strip() Remove leading and trailing whitespace.
str.lstrip() Remove leading whitespace.
str.rstrip() Remove trailing whitespace.
s1.find(s2) Finds string s2 in string s1
str.split() Split str by the whitespace characters.
str.split(sep) Split str using sep as the delimiter.

5
String Formatting: str.format()

• The str.format() method allows variable substitutions


and value formatting.
• Format strings contain “replacement fields” surrounded by {}.
• Anything that is not contained in braces is considered literal text.
'{} {}'.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(10,20,30) #-> 10 20 30

print('{:4d}{:4d}{:4d}'.format(1,2,3)) #-> 1 2 3
print('{:4d}{:4d}{:4d}'.format(10,20,30)) #-> 10 20 30

6
String Formatting: f-strings
The latest versions of Python include f-strings (f”…”)
allowing formatting strings in place. The string starts with f.
• Format strings contain “replacement fields” surrounded by {}.
• Anything that is not contained in braces is considered literal text.
f"{'one'} {'two'}" #-> 'one two'
f"{'test':*>10}" #-> '******test'
f'{42:04d}' #-> '0042'
f'{3.2451:5.2f}' #-> ' 3.25'

print(1,2,3) #-> 1 2 3
print(10,20,30) #-> 10 20 30

print(f'{1:4d}{2:4d}{3:4d}') #-> 1 2 3
print(f'{10:4d}{20:4d}{30:4d}') #-> 10 20 30

7
Math functions
• Python has a math module that provides most of the
familiar mathematical functions. Exist an equivalent cmath
module to work with complex numbers.
• A module is a Python file that defines a collection of
related functions and objects.
• Before using a module, it should be imported:
>>> import math
>>> import cmath
• To access one of the functions, specify the name of the
module and the name of the function, separated by a dot.
>>> degrees = 45
>>> radians = degrees / 360.0 * 2 * math.pi
>>> math.sin(radians)
0.707106781187 # Angle arguments are always in radians!
>>> cmath.sqrt(-1)
1j

8
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

You might also like