Python Forum
cannot insert variables {r1}, {r2}, {r3}
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
cannot insert variables {r1}, {r2}, {r3}
#2
You are confusing .format with f'. This is your example with corrections,
num1=111
num2=222
num3=333
text1 = "numbers: {0},{1},{2}"
print(text1.format(num1,num2,num3))
The numbers in "numbers: {0},{1},{2}" are index numbers and tell the formatter which argument to print where. This will print the numbers in the opposite order.
num1=111
num2=222
num3=333
text1 = "numbers: {2},{1},{0}"
print(text1.format(num1,num2,num3))
Output:
numbers: 333,222,111
The code below uses the newest way to format strings, f-Strings.
import random
r1 =(random.randrange(0, 3))
r2 =(random.randrange(3, 6))
r3 =(random.randrange(6, 9))
print(f'numbers: {r1},{r2},{r3}')
When using f-Strings the brackets contain the thing you want to print instead of an index that is used to get the value from the format() function.
Kakha likes this post
Reply


Messages In This Thread
cannot insert variables {r1}, {r2}, {r3} - by Kakha - Jan-07-2021, 08:48 PM
RE: cannot insert variables {r1}, {r2}, {r3} - by deanhystad - Jan-07-2021, 09:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Insert using psycopg giving syntax error near "INSERT INTO" olgethorpe 4 18,920 Jul-21-2017, 07:39 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020