Python Forum
Remove space after random.randrange(1, 11)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove space after random.randrange(1, 11)
#1
Hello. So, I have just started learning Python and now I would like some help.

The code works but there is a space after the last random generated number, how to remove that space?

print('What is',random.randrange(1, 11),'times',random.randrange(1, 11),'?')
Example: What is 3 times 2 ?

Is lstrip() something useable here?
Reply
#2
Your have a print statement with commas that generate blank spaces, formatting gives more control.
Perhaps you are looking for this:
print('What is {} times {}?'.format(random.randrange(1, 11),random.randrange(1, 11)))
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
(Jun-06-2020, 06:44 AM)DPaul Wrote: Your have a print statement with commas that generate blank spaces, formatting gives more control.
Perhaps you are looking for this:
print('What is {} times {}?'.format(random.randrange(1, 11),random.randrange(1, 11)))
Paul

Oh, thanks for fast reply, it works great. Interesting with .format, will read and test more.
Reply
#4
(Jun-06-2020, 07:01 AM)rs74 Wrote: Oh, thanks for fast reply, it works great. Interesting with .format, will read and test more.
.format() is starting to get old,there is now f-string.
from random import randrange

print(f'What is {randrange(1, 11)} times {randrange(1, 11)}?')
Output:
What is 4 times 1?
Reply
#5
(Jun-06-2020, 07:21 AM)snippsat Wrote:
(Jun-06-2020, 07:01 AM)rs74 Wrote: Oh, thanks for fast reply, it works great. Interesting with .format, will read and test more.
.format() is starting to get old,there is now f-string.
from random import randrange

print(f'What is {randrange(1, 11)} times {randrange(1, 11)}?')
Output:
What is 4 times 1?

Great, thank you. Thumbs Up
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,430 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Remove a space between a string and variable in print sie 5 1,706 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  from global space to local space Skaperen 4 2,269 Sep-08-2020, 04:59 PM
Last Post: Skaperen
  How to remove space between strings sunnyarora 2 2,673 May-03-2019, 11:44 AM
Last Post: perfringo
  random.randrange ravioli2929 14 6,685 Mar-17-2019, 04:49 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

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