Skip to content

Instantly share code, notes, and snippets.

@cadu-leite
Last active June 26, 2020 21:17
Show Gist options
  • Save cadu-leite/bd0c267797a6d20a64fe69780d6e3406 to your computer and use it in GitHub Desktop.
Save cadu-leite/bd0c267797a6d20a64fe69780d6e3406 to your computer and use it in GitHub Desktop.
import math
s = 'Sample text'

import decimal
dn = decimal.Decimal(math.pi) # a decimal number
fn = math.pi  # a float number

print (f'{s}')
print (f'{dn}')
print (f'{fn}')

saída ...

Sample text
3.141592653589793115997963468544185161590576171875
3.141592653589793
print(f'{s}')

saída ...

Sample text
print(f'{s!r}')

saída ...

'Sample text'
print(f'{s!s}')

saída ...

Sample text
print(f'{s!a}')

saída ...

'Sample text'
s = '\N{SNAKE}'  # `/N` pick a Unicode string by name
print(f'{s!r}')  # access the __repr__ method
print(f'{s!s}')  # the __str__ method

saída ...

'🐍'
🐍

F-Strings text formating

s = 'Python \N{SNAKE}'

justify RIGHT (<-) with " " (spaces) completing the width

print(f'->|{s:>30}|<-')  # justify right with "<spaces>" completing the width

saída ...

->|                      Python 🐍|<-

justify LEFT (->) with “.” dots completing the width

print(f'->|{s:.<30}|<-')  # justify left with "." dots completing the width

saída ...

->|Python 🐍......................|<-
print(f'->|{s:.>30}|<-')

saída ...

->|......................Python 🐍|<-
print(f'->|{s:^30}|<-')  # center

saída ...

->|           Python 🐍           |<-

Format and limiting string length

print(f'->|{s:^30.2}|<-')  # center & limit

saída ...

->|              Py              |<-

its possibe to use variables

w = 20
print(f'->|{s:.>{w}}')

saída ...

->|............Python 🐍
print(f'->|{s:.^{w}}')

saída ...

->|......Python 🐍......
w = 30.2
print(f'->|{s:.^{w}}')

saída ...

->|..............Py..............

F-Strings Number formats

pi = math.pi
type(pi)

saída ...

float
print(f'{pi}')

saída ...

3.141592653589793
print(f'({pi:10.3f})')

saída ...

(     3.142)
width=10
precision= 4

print(f'({pi:{width}.{precision}f})')

saída ...

(    3.1416)
print(f'{pi:}')

saída ...

3.141592653589793

Leading Zeros

print(f"{1:05}")

saída ...

00001
print(f"{15:05}")

saída ...

00015
print(f"{1.5:05}")

saída ...

001.5
import decimal
print(f"{decimal.Decimal(1.5):05}")

saída ...

001.5
width=10
precision= 4
print(f'({pi:{width}.{precision}f})')

saída ...

(    3.1416)
width=10
precision= 4

sr = f'{pi:{width}.{precision}f}'
sr

saída ...

'    3.1416'
f'{pi:30}'

saída ...

'             3.141592653589793'
f'{pi:030}'

saída ...

'00000000000003.141592653589793'
big_number = 98765432123456789
f"{big_number:_}"

saída ...

'98_765_432_123_456_789'
f'{pi:.2%}'

saída ...

'314.16%'
print(f'{decimal.Decimal(4.4):05.2}')

saída ...

004.4
print(f'{decimal.Decimal(4.4):05.5}')

saída ...

4.4000
print(f'->|\n|')

saída ...

->|
|
print(f'->|\r|')

saída ...

|
print('\N{SNOWMAN}')

saída ...

print('\N{THUNDERSTORM}')

saída ...

'\N{PERSON WITH BALL}'

saída ...

'⛹'
'\N{HAPPY PERSON RAISING ONE HAND}'

saída ...

'🙋'
'\N{HIRAGANA LETTER KA}\N{HIRAGANA LETTER DU}'

saída ...

'かづ'
'\N{FLEUR-DE-LIS}'

saída ...

'⚜'
'\N{YIN YANG}'

saída ...

'☯'
'\N{SNAKE}'

saída ...

'🐍'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment