String Formatting is a tutorial that covers the basics of formatting strings in Python. The tutorial begins by defining what string formatting is and why it is useful in programming. It explains how to format strings in Python using different methods, including using the percent symbol and using the format() method.


# Tuple = A fixed size list
name = "John"
print("Hello, %s!" % name)

# A string (%s) and an Integer (%d)
name = "John"
age = 23
print("%s is %d years old." % (name, age))

The tutorial covers a wide range of formatting options, including specifying precision and width, formatting numbers, and working with dates and times. It also covers string formatting with dictionaries and advanced formatting options, such as alignment, padding, and truncation.


# This prints out: A list: [1, 2, 3]
mylist = [1,2,3]
print("A list: %s" % mylist)

Throughout the tutorial, there are numerous practical examples and exercises to help reinforce the concepts covered. These examples cover a range of real-world scenarios, such as formatting strings for output, working with CSV files, and formatting log messages.

The tutorial concludes by introducing some advanced topics related to string formatting, including f-strings, which are a more modern and concise way to format strings in Python, and using the string. Template class for more complex formatting tasks.

String Formatting Exercise Solution


#Code Completed
data = ("John", "Doe", 53.44)
format_string = "Hello %s %s. Your current balance is $%s."

print(format_string % data)

Overall, String Formatting is an excellent resource for anyone looking to learn the basics of formatting strings in Python. Its clear and concise explanations, practical examples, and interactive exercises make it an ideal resource for beginners and intermediate programmers alike. Whether you are looking to format strings for output or perform more complex formatting tasks in Python, this tutorial is an excellent starting point.