Python Forum
Adding spaces to digits
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding spaces to digits
#2
There are a few different ways to do this and your code can be made much more succinct.

This (for example) removes the hyphens and reverses each 'token'. The resulting tokens are replaced, within the same list object.

ccn = "2323-2005-7766-3554"
ccn_lst = ccn.split("-")

for index, token in enumerate(ccn_lst):
    ccn_lst[index] = token[::-1]
Have a look into string slicing to see how this works and to complete the rest of your project.

to add: you could also look into the third perimeter of the range() function, which is commonly called a 'step': range(start, stop, step).
deadkill02 likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply


Messages In This Thread
Adding spaces to digits - by deadkill02 - Feb-08-2024, 08:12 PM
RE: Adding spaces to digits - by rob101 - Feb-08-2024, 09:22 PM
RE: Adding spaces to digits - by rob101 - Feb-09-2024, 04:51 PM
RE: Adding spaces to digits - by deanhystad - Feb-09-2024, 07:29 PM
RE: Adding spaces to digits - by Pedroski55 - Feb-10-2024, 07:02 AM
RE: Adding spaces to digits - by rob101 - Feb-10-2024, 01:08 PM

Forum Jump:

User Panel Messages

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