formatted string python 3 code example

Example 1: f string in python

# f-strings are short for formatted string like the following
# you can use the formatted string by two diffrent ways
# 1
name = "John Smith"
print(f"Hello, {name}")		# output = Hello, John Smith

# 2
name = "John Smith"
print("Hello, {}".format(name))		# output = Hello, John Smith

Example 2: formatted string python

# Newer f-string format
name = "Foo"
age = 12
print(f"Hello, My name is {name} and I'm {age} years old.")
# output :
# Hello, my name is Foo and I'm 12 years old.

Example 3: python f string

>>> name = "Eric"
>>> age = 74
>>> f"Hello, {name}. You are {age}."
'Hello, Eric. You are 74.'

Example 4: python fstring

#python3.6 is required
age = 12
name = "Simon"
print(f"Hi! My name is {name} and I am {age} years old")

Example 5: new python string formatting

>>> f'Hello, {name}!'
'Hello, Bob!'

Example 6: str.format python 3

print("Sammy ate {0:f} percent of a {1}!".format(75, "pizza"))