1

I just started learning Python and I want to convert one of my codes to this language. I have a little problem what I can't solve. My script have to make random numbers and printing them after another without adding them together.

time = random.randint(1000000000,9999999999)
no_hash = time+random.randint(1,100)+random.randint(1,10000)+random.randint(1,10000000)

Example output 4245200583 but I need just like this: 423694030332415251234. As you see the code adding the numbers together (because of + between them) but I don't want to add the numbers together just print them.

3
  • 1
    cast to string, append to string? Commented Feb 19, 2021 at 15:15
  • 1
    In what languages would you expect this to work? Commented Feb 19, 2021 at 15:16
  • In Python I want to make it. Commented Feb 19, 2021 at 15:17

3 Answers 3

2

Each of your values is an int, so + will implement integer addition. You need to convert each int to a str first.

You can do that implicitly with various string-formatting operations, such as

time = random.randint(1000000000,9999999999)
no_hash = f'{time}{random.randint(1,100)}{random.randint(1,10000)}{random.randint(1,10000000)}'

or explicitly to be joined using ''.join:

time = random.randint(1000000000,9999999999)
no_hash = ''.join([str(x) for x in [time, random.randint(1, 100), random.randint(1,10000), random.randint(1,1000000)]])
Sign up to request clarification or add additional context in comments.

Comments

0

use str() to concatenate the numbers

time = random.randint(1000000000,9999999999)
no_hash = str(time)+str(random.randint(1,100))+str(random.randint(1,10000))+str(random.randint(1,10000000))

2 Comments

Repeated concatenation with + is probably the worst way to join multiple values into a single string.
@TheMineSlash this method can also be used: stackoverflow.com/a/42149618/14719340
0

You are adding a type of integer, when you need to add a type of string.

use the str() declaration to make sure Python sees it as string.

For example:

time = random.randint(1000000000,9999999999)
no_hash = str(time)+str(random.randint(1,100))+str(random.randint(1,10000))+str(random.randint(1,10000000))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.