<jupyter_start><jupyter_text>Python String FormattingThe format() method allows you to format selected parts of a string.Sometimes there are parts of a text that you do not control, maybe they come from a database, or user input?To control such values, add placeholders (curly brackets {}) in the text, and run the values through the format() method:<jupyter_code>price = 49
txt = "The price is {} dollars"
print(txt.format(price))
# Format the price to be displayed as a number with two decimals:

txt = "The price is {:.2f} dollars"
txt.format(price)
# Multiple Values

quantity = 3
itemno = 567
price = 49
myorder = "I want {} pieces of item number {} for {:.2f} dollars."
print(myorder.format(quantity, itemno, price))
# Index Numbers

quantity = 3
itemno = 567
price = 49
myorder = "I want {0} pieces of item number {1} for {2:.2f} dollars."
print(myorder.format(quantity, itemno, price))<jupyter_output><empty_output><jupyter_text>Examples<jupyter_code>age = 36
name = "John"
txt = "His name is {1}. {1} is {0} years old."
print(txt.format(age, name))<jupyter_output><empty_output><jupyter_text>Named IndexesYou can also use named indexes by entering a name inside the curly brackets {carname}, but then you must use names when you pass the parameter values txt.format(carname = "Ford"):<jupyter_code>myorder = "I have a {carname}, it is a {model}."
print(myorder.format(carname = "Ford", model = "Mustang"))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11218836565096953, 'fraction_numerical': 0.01731301939058172, 'mean_word_length': 5.509009009009009, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '358551', 'license': 'MIT', 'n_tokens_mistral': 492, 'n_tokens_neox': 457, 'n_words': 178, 'path': 'Python/string_formatting.ipynb', 'repo_name': 'JSJeong-me/RaspberryPI', 'size': '1444'}