+ 1

How to insert randomint inside a string

For example I have a string that consists of 10 random integers, and through input I want to Cross out one of them (it becomes “—“ instead of “number” Was: “1,40,33,10,34,90,91....” Which one do want to cross out? : 34 Became: “1,40,33,10,—,90,91...”

5th Sep 2019, 9:56 AM
Oscar
Oscar - avatar
11 Answers
+ 5
Here is the code: s = "1, 40, 33, 10, 34, 90..." num = input('Your number: ") if(num in s): s = s.replace(num, "--")
5th Sep 2019, 10:17 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
I still don't understand your question very well, you want to change number 34 with a randint generated number ?
5th Sep 2019, 10:00 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
This is what I understood: You have a string, say s = "1,40,33,10..." Let's say user input is 40 The string should become: "1,--,33,10..." ?
5th Sep 2019, 10:06 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
If you are working with strings, you should use the .choice() function from random module to pick a random character from a set of string
5th Sep 2019, 10:09 AM
Rex Fajardo
Rex Fajardo - avatar
+ 1
Aymane Boukrouh thank you!
5th Sep 2019, 10:18 AM
Oscar
Oscar - avatar
+ 1
Oscar anytime!
5th Sep 2019, 10:19 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
0
Aymane Boukrouh , no. i want to change the number a user inputs with a string “—“ sort of like when cross out numbers in lottery
5th Sep 2019, 10:01 AM
Oscar
Oscar - avatar
0
My point is, why use a string ? Can't you use a list ?
5th Sep 2019, 10:10 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
0
no because i have an assignment that says i HAVE to use a string...
5th Sep 2019, 10:10 AM
Oscar
Oscar - avatar
0
Aymane Boukrouh and yes, you understood exactly right
5th Sep 2019, 10:11 AM
Oscar
Oscar - avatar
7th Sep 2019, 7:13 AM
Sousou
Sousou - avatar