# Basic usage — ALWAYS returns a string!
name = input('Enter your name: ')
print(f'Hello, {name}!')
# Need a number? Convert it!
age = int(input('Enter age: '))
height = float(input('Enter height in meters: '))
# Multiple inputs on one line
# User types: John 25
name, age = input('Name and age: ').split()
# With specific separator
# User types: John,25,Engineer
name, age, role = input('Enter (comma-separated): ').split(',')
# Map to types directly
# User types: 10 20 30
nums = list(map(int, input('Numbers: ').split()))
print(sum(nums)) # 60Critical: input() ALWAYS returns str. If you need any other type, you MUST convert.
name = 'Alice'
age = 30
pi = 3.14159
# Basic interpolation
print(f'Name: {name}, Age: {age}')
# Expressions inside {}
print(f'Next year: {age + 1}')
print(f'Double pi: {pi * 2}')
# Format specs
print(f'Pi: {pi:.2f}') # 3.14
print(f'Pi: {pi:.4f}') # 3.1416
print(f'Pi: {pi:10.2f}') # ' 3.14' (width 10)
print(f'Number: {42:05d}') # '00042' (zero-padded)
print(f'Hex: {255:x}') # 'ff'
print(f'Binary: {10:b}') # '1010'
print(f'Percent: {0.85:%}') # '85.000000%'
print(f'Percent: {0.85:.1%}') # '85.0%'
# Alignment
print(f'{"Left":<20}') # 'Left '
print(f'{"Center":^20}') # ' Center '
print(f'{"Right":>20}') # ' Right'
# Thousand separators
print(f'{1000000:,}') # '1,000,000'
print(f'{1000000:_}') # '1_000_000'
# Calling methods inside f-strings
print(f'{name.upper()}') # 'ALICE'
print(f'{"hello world".title()}') # 'Hello World'
# Nested f-strings (Python 3.12+)
# width = 10
# print(f'{"hi":{width}}')
# Debugging with = (Python 3.8+)
x = 42
print(f'{x=}') # 'x=42'
print(f'{x + 1=}') # 'x + 1=43'
print(f'{name=!r}') # "name='Alice'" (repr format)# Positional
print('{} loves {}'.format('Alice', 'Python'))
print('{0} loves {1}, {0} loves {1}'.format('Alice', 'Python'))
# Named
print('{name} is {age}'.format(name='Alice', age=30))
# With format specs
print('{:.2f}'.format(3.14159)) # '3.14'
print('{:05d}'.format(42)) # '00042'name = 'Alice'
age = 30
print('Name: %s, Age: %d' % (name, age))
print('Pi: %.2f' % 3.14159)
print('%05d' % 42) # '00042'
# Dictionary style
print('%(name)s is %(age)d' % {'name': 'Alice', 'age': 30})Use f-strings unless maintaining legacy code.
Create a simple calculator program: take two numbers and an operator (+, -, *, /) as input. Print the result formatted to 4 decimal places if it's a float, or as an integer if the result is whole. Handle division by zero gracefully. Use f-strings with conditional format specs.