Replace Python String Order

Asked 2 years ago, Updated 2 years ago, 15 views

I made this code.

def Replace place (a):
    b = ''
    for i in range (0, len(a)-1,2):
        b += a[i+1] + a[i]
    return b

If you change the position of '1234567' here, there is a problem that 563412 comes out and the last digit disappears. I want to print it out until the last digit, is there any way?

python

2022-09-20 19:19

1 Answers

>>> def f(a):
    b = ''
    for i in range(0, len(a)-1, 2):
        b += a[i+1]+a[i]
    return b

>>> f("1234567")
'214365'
>>> def f(a):
    b = ''
    for i in range(0, len(a)-1, 2):
        b = a[i+1]+a[i]+b
    return b

>>> f("1234567")
'654321'
>>> def f(a):
    b = ''
    for i in range(0, len(a)-1, 2):
        b = a[i]+a[i+1]+b
    return b

>>> f("1234567")
'563412'
>>> def f(a):
    b = ''
    for i in range(0, len(a)+1, 2):
        b = a[i:i+2]+b
    return b

>>> f("1234567")
'7563412'
>>> 


2022-09-20 19:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.