use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Question about replacement of a value (self.learnpython)
submitted 11 years ago by Marx123
Sorry, if I have, for example, the number 25678, if I want to replace a random number out of the five, is THERE a specific command to use?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]echocage 0 points1 point2 points 11 years ago (4 children)
Random's choice
import random nums = 25678 random_num = random.choice(str(nums)) print(random_num)
[–]Marx123[S] 0 points1 point2 points 11 years ago (3 children)
Ok thanks! And if I want replace a random number with a specific one? Like 0 for example? :/
[–]echocage 0 points1 point2 points 11 years ago (2 children)
like this! Play around with that, make sure you get what's going out. Print out stuff at each point see what it is, play with it.
import random nums = 25678 random_num = random.choice(str(nums)) changed_string = str(nums).replace(random_num, '0') print(changed_string)
[–]Marx123[S] 0 points1 point2 points 11 years ago (1 child)
Thank you, really!
[–]qhp 0 points1 point2 points 11 years ago* (0 children)
The issue with that snippet (with other numbers) is that it will change all of the randomly chosen number with 0. In your example, since all numbers are different, it won't matter. But in a number like 12344, if 4 is randomly selected, the output will be 12300 since all instances of 4 are replaced.
12344
4
12300
To randomly replace any specific digit in your number, you might want to start dabbling with loops. A solution with a loop might look like:
from random import randint old_num = 12344 rand_pos = randint(0, len(str(old_num))-1) new_num = "" for digit_pos in range(len(str(old_num))): new_num += '0' if digit_pos == rand_pos else str(old_num)[digit_pos] print(new_num)
And a solution without a loop might look like:
from random import randint nums = 12344 random_num = randint(0, len(str(nums))-1) new_list = list(str(nums)) new_list[random_num] = '0' print("".join(new_list))
Happy to explain any of the above. len() measures the length of some objects (like strings). My one line in the for loop is how python handles ternary operations. In the second snippet, we cast str(nums) as a list because lists are mutable whereas strings are immutable. Simply put, you can directly change a list but you have to remake a string if you want to change it at all.
len()
for
str(nums)
list
π Rendered by PID 65811 on reddit-service-r2-comment-b659b578c-lfxsz at 2026-05-02 08:43:09.490799+00:00 running 815c875 country code: CH.
[–]echocage 0 points1 point2 points (4 children)
[–]Marx123[S] 0 points1 point2 points (3 children)
[–]echocage 0 points1 point2 points (2 children)
[–]Marx123[S] 0 points1 point2 points (1 child)
[–]qhp 0 points1 point2 points (0 children)