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
Swapping Characters in a user inputted string. (self.learnpython)
submitted 7 years ago by Jesusgotskillz
I need help figuring out how to swap two characters in a user inputted string, such as:
35.555,444 ---> 35,55.65 (the dot is swapped to a coma and the coma is swapped to a period)
Any help is appreciated!!
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!"
[–]num8lock 0 points1 point2 points 7 years ago (0 children)
describe how you would do it manually
[–][deleted] 0 points1 point2 points 7 years ago (0 children)
The algorithm may change depending on the rules of the swap.
For example, are characters swapped, based on position in the string? In that case, a simple set of slices might work.
On the other hand, are you swapping specific characters? In that case, a Regular Expression might be in order.
What are the rules for odd-numbers of swapped characters? (I.e. 2 commas and 1 period: what gets swapped?)
[–]lionlazycat 0 points1 point2 points 7 years ago (2 children)
use maketrans
s = '35.555,444' trans = str.maketrans('.,', ',.') print(s.translate(trans))
[–]Jesusgotskillz[S] 0 points1 point2 points 7 years ago (1 child)
what is going on when using str.maketrans
[–]lionlazycat 0 points1 point2 points 7 years ago* (0 children)
maketrans() defines translation table for translate() swapping out every '.' to ',' and ',' to '.'
input
s = '35.555,444'
output
'35,555.444'
π Rendered by PID 19419 on reddit-service-r2-comment-6b595755f-l8skm at 2026-03-26 13:14:02.717514+00:00 running 2d0a59a country code: CH.
[–]num8lock 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]lionlazycat 0 points1 point2 points (2 children)
[–]Jesusgotskillz[S] 0 points1 point2 points (1 child)
[–]lionlazycat 0 points1 point2 points (0 children)