Thinking Like a Programmer•Interactive notebook lesson•Free
String formatting
No account is required. Learn the material, try the examples, and mark it complete when you are ready to move on. Sign in with email only if you want to save progress across devices.
String formatting basics
Codepython
string1 = "This is a apple" # Creating a string
print(string1)Output
This is a apple
Codepython
fruit = "apple" # Assigning the string "apple" to the variable 'fruit'
string2 = "This is a {}".format(fruit)
# The '{}' is replaced with the value of 'fruit' using .format()
print(string2) # Output: This is a apple
Output
This is a apple
Codepython
fruit = "apple" # Assigning the string "apple" to the variable 'fruit'
string2 = f"This is a {fruit}"
# The '{}' is replaced with the value of 'fruit' using f-string formatting
print(string2) # Output: This is a apple
Output
This is a apple
Codepython
name = "Alice" # Assigning the string "Alice" to the variable 'name'
age = 25 # Assigning the integer 25 to the variable 'age'
# Using an f-string to format the string dynamically
formatted_string = f"My name is {name} and I am {age} years old."
# Printing the formatted string
print(formatted_string) # Output: My name is Alice and I am 25 years old.
Output
My name is Alice and I am 25 years old.
Codepython
name = "Bob" # Assigning the string "Bob" to the variable 'name'
age = 30 # Assigning the integer 30 to the variable 'age'
# Using the .format() method to insert values into the string
formatted_string = "My name is {} and I am {} years old.".format(name, age)
# Printing the formatted string
print(formatted_string) # Output: My name is Bob and I am 30 years old.
Output
My name is Bob and I am 30 years old.
Codepython
# Assigning values to variables
name = "Bob" # The person's name
age = 30 # The person's age
# Using the .format() method with positional indexing
# {1} refers to the second argument (name)
# {0} refers to the first argument (age)
formatted_string = "My name is {1} and I am {0} years old.".format(age, name)
# Printing the formatted string
print(formatted_string) # Output: My name is Bob and I am 30 years old.
Output
My name is Bob and I am 30 years old.
Codepython
# Define a string with placeholders {n} and {a} for dynamic insertion of values
formatted_string = "My name is {n} and I am {a} years old.".format(n=name, a=age)
# The .format() method is used to replace the placeholders with the values of 'name' and 'age'
# 'n' corresponds to the 'name' variable, and 'a' corresponds to the 'age' variable
# Print the resulting formatted string to the console
# The output will be: "My name is <value of name> and I am <value of age> years old."
print(formatted_string) # Display the final formatted string
Output
My name is Bob and I am 30 years old.
Codepython
# Assign the name and age values to variables
name = "Charlie"
age = 35
# Use old-style string formatting with % operator
# %s is used for inserting a string (name), and %d is used for inserting an integer (age)
formatted_string = "My name is %s and I am %d years old." % (name, age)
# Print the formatted string with the values inserted in place of %s and %d
print(formatted_string) # Output: "My name is Charlie and I am 35 years old."
Output
My name is Charlie and I am 35 years old.
Codepython
# Define a list of words
words = ["Hello", "World"]
# Use the .join() method to concatenate the elements of the list with a space (" ") between them
# The .join() method joins each element of the list into a single string
sentence = " ".join(words)
# Print the concatenated sentence
# The result will be: "Hello World"
print(sentence)
Output
Hello World
Codepython
# Assign the value of pi
pi = 3.1415926535
# Use f-string formatting to round pi to 2 decimal places
# The syntax {pi:.2f} means format the value of 'pi' with 2 decimal places
print(f"Pi rounded to 2 decimals: {pi:.2f}")
# Use .format() method to round pi to 3 decimal places
# The syntax {:.3f} means format the value of 'pi' with 3 decimal places
print("Pi rounded to 3 decimals: {:.3f}".format(pi))
Output
Pi rounded to 2 decimals: 3.14
Pi rounded to 3 decimals: 3.142
Codepython
Finished this lesson?
Keep your place here, or save it across devices with an email link.
Saved in this browser. Save progress with email →