← Back to list

Python User Input and String Formatting: A Beginner’s Guide

Learn how to accept user input and format strings in Python using input(), f-strings, and format() with examples and syntax breakdowns.

codingsprints in Python in Plain English · 2025-04-24 14:31 · 1 claps · 4.8 min read paywalled
#python #user-input #python-f-strings #input-output #string-formatting
Open on Medium ↗
Wiki topics: GEN · Genomics & Sequencing PFI · Personal Finance LNG · Linguistics & Language

Python User Input and String Formatting: A Beginner’s Guide

Learn how to accept user input and format strings in Python using input(), f-strings, and format() with examples and syntax breakdowns.

Master user input handling and powerful string formatting in Python with real-world examples.

Python User Input and String Formatting

Python User Input and String Formatting

Taking User Input in Python

Python allows you to take input from users using the **input()** function. This is especially useful for interactive programs.

Python 3.x Example:

username = input("Enter username:")
print("Username is: " + username)

Python 2.7 Equivalent:

⚠️ Python 2 is outdated. Always use Python 3 for new projects.

The program pauses and waits for input. Once the user hits Enter, execution resumes.

String Formatting in Python

Python offers powerful tools to format strings for clean output. The most modern way is to use F-Strings, available in Python 3.6+.

What is an F-String?

F-strings are string literals prefixed with an f. They allow embedding expressions directly using **{}**.

txt = f"The price is 49 dollars"
print(txt) # The price is 49 dollars

Using Variables in F-Strings

To format values in an f-string, add placeholders {}, a placeholder can contain variables, operations, functions, and modifiers to format the value.

Example:

price = 59
txt = f"The price is {price} dollars"
print(txt) # The price is 59 dollars

Format Numbers with Modifiers

A modifier is included by adding a colon **: followed by a legal formatting type, like `.2f`** which means fixed point number with 2 decimals:

price = 59
txt = f"The price is {price:.2f} dollars"
print(txt) # The price is 59.00 dollars

You can also format a value directly without keeping it in a variable

txt = f"The price is {95:.2f} dollars"
print(txt) # The price is 95.00 dollars

Perform Operations in F-Strings

You can perform Python operations inside the placeholders

You can do math operations:

txt = f"The price is {20 * 59} dollars"
print(txt) # The price is 1180 dollars

You can perform math operations on variables

price = 59
tax = 0.25
txt = f"The price is {price + (price * tax)} dollars"
print(txt) # The price is 73.75 dollars

You can perform **if...else** statements inside the placeholders:

price = 49
txt = f"It is very {'Expensive' if price>50 else 'Cheap'}"

print(txt) # It is very Cheap

Call Functions Inside F-Strings

You can execute functions inside the placeholder

fruit = "apples"
txt = f"I love {fruit.upper()}"
print(txt) # I love APPLES

The function does not have to be a built-in Python method, you can create your functions and use them

def myconverter(x):
  return x * 0.3048

txt = f"The plane is flying at a {myconverter(30000)} meter altitude"
print(txt) # The plane is flying at a 9144.0 meter altitude

Other Common Format Specifiers

At the beginning of this chapter, we explained how to use the .2f modifier to format a number into a fixed-point number with 2 decimals.

Several other modifiers can be used to format values

price = 59000
txt = f"The price is {price:,} dollars"
print(txt) # The price is 59,000 dollars

Here is a list of all the formatting types.

  • **:<** – Left aligns the result (within the available space)
  • **:>** – Right-aligns the result (within the available space)
  • **:^** – Center aligns the result (within the available space)
  • **:=** – Places the sign in the leftmost position
  • **:+** – Use a plus sign to indicate if the result is positive or negative
  • **:-** – Use a minus sign for negative values only
  • **:** – Use a space to insert an extra space before positive numbers (and a minus sign before negative numbers)
  • **:,** – Use a comma as a thousand separator
  • **:_** – Use an underscore as a thousand separator
  • **:b** – Binary format
  • **:c** – Converts the value into the corresponding Unicode character
  • **:d** – Decimal format
  • **:e** – Scientific format, with a lowercase 'e'
  • **:E** – Scientific format, with an uppercase 'E'
  • **:f** – Fixed point number format
  • **:F – Fixed point number format, in uppercase (shows `inf** andnan` as **INF and `NAN`**)
  • **:g** – General format
  • **:G** – General format (uses uppercase 'E' for scientific notation)
  • **:o** – Octal format
  • **:x** – Hex format, lowercase
  • **:X** – Hex format, uppercase
  • **:n** – Number format
  • **:%** – Percentage format

Using .format() Method (Pre Python 3.6)

The **format()** method can still be used, but f-strings are faster and the preferred way to format strings

The **format()** method also uses curly brackets as placeholders {}But the syntax is slightly different:

price = 49
txt = "The price is {} dollars"
print(txt.format(price)) # The price is 49 dollars

You can add parameters inside the curly brackets to specify how to convert the value

price = 49
txt = "The price is {:.2f} dollars"
print(txt.format(price)) # The price is 49.00 dollars

Multiple Values

If you want to use more values, just add more values to the format() method

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

# I want 3 pieces of item number 567 for 49.00 dollars.

Index Numbers

You can use index numbers (a number inside the curly brackets **{0}**) to be sure the values are placed in the correct placeholders.

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))

# I want 3 pieces of item number 567 for 49.00 dollars.

Also, if you want to refer to the same value more than once, use the index number

age = 36
name = "John"
txt = "His name is {1}. {1} is {0} years old."
print(txt.format(age, name))

# His name is John. John is 36 years old.

Named Indexes

You 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")`**

myorder = "I have a {carname}, it is a {model}."
print(myorder.format(carname = "Ford", model = "Mustang"))

# I have a Ford, it is a Mustang.

Wrapping Up

Python’s **input() and string formatting tools like f-strings and `.format()`** Make writing interactive, readable code a breeze. With these tools, you can make dynamic, user-friendly programs.

If you enjoyed this guide, claps and comments are appreciated! 💬👏

Follow CodingSprints for more hands-on Python tutorials.

📲 Stay connected: 📘 Facebook | 🐦 Twitter | 💻 GitHub | 🔗 LinkedIn

Thank you for being a part of the community

Before you go:


메타데이터
post_id
dcd7fc57c80e
slug
python-user-input-and-string-formatting-a-beginners-guide-dcd7fc57c80e
url
https://python.plainenglish.io/python-user-input-and-string-formatting-a-beginners-guide-dcd7fc57c80e
canonical_url
https://python.plainenglish.io/python-user-input-and-string-formatting-a-beginners-guide-dcd7fc57c80e
author_url
https://medium.com/@codingsprints
status
ok
fetched_at
2026-08-16 17:22:32