Python — Variables, Strings, and User Input

Variables and Assignment

Variables assign a label to a value. Python is dynamically typed — no type declaration needed.

message = "Hello world!"
count = 42
price = 3.14
print(message)

Strings

A string is a series of characters in single or double quotes. Both are equivalent.

name = "Albert"
name = 'Albert'   # same thing

f-strings

f-strings embed variable values directly into a string using {}. Prefix with f.

first_name = 'albert'
last_name  = 'einstein'
full_name  = f"{first_name} {last_name}"
print(full_name)          # albert einstein
print(full_name.title())  # Albert Einstein

User Input

input() prompts the user and returns their response as a string. Convert to a number type when needed.

# String input
name = input("What's your name? ")
print(f"Hello, {name}!")
 
# Integer input
age = input("How old are you? ")
age = int(age)                      # string → int
 
# Float input
pi = input("What's the value of pi? ")
pi = float(pi)                      # string → float

All input() returns a string

Forgetting to convert with int() or float() before arithmetic is the most common beginner bug. Always convert before comparing numbers or doing math.

Cross-References

graph TD
    A[Variables] --> B[Strings: single or double quotes]
    A --> C["Numbers: int / float"]
    B --> D["f-strings: embed {values} in text"]
    C --> E["input() returns string → convert with int/float"]
    D --> F[print output]
    E --> F