String formatting

import datetime

name = "Eric"
age = 74
print(f"Hello, {name}. You are {age}.")

list_name = ["Niclas", "Anna", "Sofie"]
print(f"Hej , master {list_name[0]} och drottning {list_name[1]} och slav {list_name[2]}")

#********************************************************
print("") # Just a spacer

new_comedian = ("Eric", "Idle", "74")
print(f"{new_comedian}")

#********************************************************
print("") # Just a spacer

y = "pythonistas"
print('We all are {}.'.format('equal'))
print('We all are {}.'.format(y))

#********************************************************
print("") # Just a spacer

x = 'looked'
print("Misha %s and %s around" %('walked',x))

#********************************************************
print("") # Just a spacer

#****************** Floating numbers ************************
print('The value of pi is: %5.4f' %(3.141592))
print('Floating point numbers: %1.0f' %(13.144))

float_number = 3.141592
print(f'Pi with two decimals {float_number:.2f}')
print(f'with no decimals... {float_number:.0f}')

#********************************************************
print("") # Just a spacer


#************* Calculate two numbers **********************
int_a = 10
int_b = 13
int_c = 5
print(f'{int_a + int_b = }')

#********************************************************
print("") # Just a spacer

#*********** Print variable name and value ***************
my_string = "Python"
print(f'{my_string = }')

#********************************************************
print("") # Just a spacer

number = 0.9124325345
print(f'Percentage format for number with two decimal places: {number:.6%}')
# Returns
# Percentage format for number with two decimal places: 91.24%

#********************************************************
print("") # Just a spacer

number = 0.9124325345
print(f'Fixed point format for number with three decimal places: {number:.3f}')
# Returns
# Fixed point format for number with three decimal places: 0.912

#********************************************************
print("") # Just a spacer

x = "nicLaS"
print(x)
print("x.capitalize() => " + x.capitalize()) # Niclas
print("x.lower() => " + x.lower()) # niclas
print("x.upper() => " + x.upper()) # NICLAS
print("x.center(12, '0') => " + x.center(12, '0')) # 000nicLaS000
print("x.isalnum() => " + str(x.isalnum())) # True
print("x.islower() => " + str(x.islower())) # False
print("x.isupper() => " + str(x.isupper())) # False
print("x.replace('a', '*') => " + str(x.replace('a', '*'))) # nicL*S
print("x.count('c') => " + str(x.count('c'))) # 1
print("x.index('i') => " + str(x.index('i'))) # 1
print("x.split('L') => " + str(x.split('L'))) # ['nic', 'aS']

#********************************************************
print("") # Just a spacer


f_string = 'Python'
print('Align text')
print(f'{f_string: >12}; Some more text')
print(f'{f_string: <12}; Some more text')
print(f'{f_string: ^12}; Some more text')
print('')
print('Fill empty spaces')
print(f'{f_string:_>12}; Some more text')
print(f'{f_string:+<12}; Some more text')
print(f'{f_string:*^12}; Some more text')

#********************************************************
print("") # Just a spacer

today = datetime.datetime.today()
print(f'{today:%d.%m.%y  %H:%M:%S}')
print(f'{today:%c}')

#********************************************************
print("") # Just a spacer

my_large_number = 1000000000000000
print(my_large_number)
easier_to_read_large_number = 1_000_000_000_000_000
print(easier_to_read_large_number)

Leave a Reply

Your email address will not be published. Required fields are marked *