String concatenation and f strings
Estimated Time: 30 minutes
Key ideas
- Using formatted strings
- Concatenating strings
String Concatenation and f-strings
When there’s a variable we want to combine with a string to print out a message, so far we’ve added the strings with +
.
name = "Mercy"
print("Hello, " + name) # Hello, Mercy
When the message is longer, this becomes more confusing and harder to type.
name = "Mercy"
print("The alarm went off at exactly 6:00 AM as it had every morning for the past five years. " + name + " began her morning and was ready to eat breakfast by 7:00 AM. The day appeared to be as normal as any other, and " + name + " was not expecting anything to change.")
There’s another way to format long text that uses variables, called f-strings.
name = "Mercy"
print(f"Hello, {name}") # Hello, Mercy
print(f"The alarm went off at exactly 6:00 AM as it had every morning for the past five years. {name} began her morning and was ready to eat breakfast by 7:00 AM. The day appeared to be as normal as any other, and {name} was not expecting anything to change.")
Instead of using +
to combine the variable and the string, we start the string with f
and we use {}
to insert the variable right inside the string. That way, there’s less confusion about quote marks and spaces.
Other f-string uses
We can also use f-strings for rounding.
one_third = 1/3
print(one_third)# 0.3333333333333333
print(f"{one_third:.2f}") # 0.33
f-strings have other formatting powers, but we’ll leave it at rounding floats for now.
Practice: f-strings
Solution: F-strings practice
first_num = float(input("enter first number: "))
second_num = float(input("enter second number: "))
# change the line below
result = first_num / second_num
print(f"the result is {result:.3f} ")