Basics · Lesson 6 of 27
Strings and f-strings
Strings are text. Some useful things you can do with them:
first = "Chirag"
last = "Kashyap"
print(first + " " + last) # Chirag Kashyap (+ joins strings)
print("ha" * 3) # hahaha (* repeats)
print(len(first)) # 6 (len counts characters)You can't join a string and a number with +. "Age: " + 21 gives an error. Convert it first with str(21), or use an f-string.
f-strings
Put f before the quotes, and write variables inside { }. This is the easiest way to build sentences:
name = "Riya"
marks = 88
print(f"{name} scored {marks} marks") # Riya scored 88 marks
print(f"Next year: {marks + 2}") # you can even do maths inside { }Your task
name = "Aman" and score = 92 are given. Using f-strings, print:
Aman scored 92 marks Name length: 4
The 4 must come from len(name).