f string in python
# Ques.1 num = int(input("Enter your number: ")) for i in range(1, 11): # print(str(num) + " × " + str(i) + " = " + str(i*num)) # you can't add variables print(f"{num} × {i} = {num*i}") # you can add variables
f string python
# ------------------- string format, f-string ---------------------------- # {} is placeholder num1 = 5 num2 = 3 print(f'{num1} times {num2} is {num1 / num2:.2f}') #2f means print to 2 decimal precision #5 times 3 is 1.67 #explicit call format() method number1 = 'One' number2 = 'Two' number3 = 'Three' # default(implicit) order default_order = "{}, {} and {}".format(number1,number2,number3) print(default_order) # One, Two and Three # order using positional argument positional_order = "{1}, {0} and {2}".format(number1,number2,number3) print(positional_order) # Two, One and Three # order using keyword argument keyword_order = "{i}, {j} and {k}".format(j=number1,k=number2,i=number3) print(keyword_order) # Three, One and Two
python f strings
''' In python, rather than adding pieces of string together, you can use f-strings to insert variables into strings. This makes your code much more easier to read. ''' # OLD - WITHOUT F-STRINGS name = 'Bob' age = 12 print('This is ' + name + ', he is ' + age + ' years old.') # NEW - WITH F-STRINGS print(f'This is {name}, he is {age} years old') # Note the f in front
Source: www.codegrepper.com