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 thingf-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 EinsteinUser 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 → floatAll input() returns a string
Forgetting to convert with
int()orfloat()before arithmetic is the most common beginner bug. Always convert before comparing numbers or doing math.
Cross-References
- PythonControlFlow — if statements that test variables
- PythonFunctions — passing variables as arguments
- PythonFileIOExceptions — exceptions from failed int() conversions
- CppProgramStructure — contrast: C++ requires explicit type declarations (static typing)
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