How to swap two characters in string in python? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

How to swap two characters in string in python?

'ab-cd' ==> 'cd-ab' | 'ba-dc'

19th Sep 2021, 11:33 AM
Mahmoud ayman
Mahmoud ayman - avatar
7 ответов
+ 3
You can for example split your string in a list and change the position of it items. After that concatenate to a string.
19th Sep 2021, 11:42 AM
JaScript
JaScript - avatar
+ 3
For it Mahmoud ayman you could learn all inbuild functions for a string. By the way this can be solved i.e. as in this code: https://code.sololearn.com/c1dyR8Nhx4zp/?ref=app
19th Sep 2021, 12:19 PM
JaScript
JaScript - avatar
+ 1
This function is universal for swaping two items in a string. That means the position of second part will be changed to the position of first part of its string etc. For this you have to set in addition the beginning of first as well as the second parts.
19th Sep 2021, 12:48 PM
JaScript
JaScript - avatar
+ 1
Store the two strings in variables named x and y, and then write: x,y=y,x
17th Oct 2021, 6:24 AM
VIVEK VATS
0
JaScript I want to do that without using list
19th Sep 2021, 11:52 AM
Mahmoud ayman
Mahmoud ayman - avatar
0
JaScript is that function to solve just my example?
19th Sep 2021, 12:27 PM
Mahmoud ayman
Mahmoud ayman - avatar
0
JaScript I get it but the function is a little confused
19th Sep 2021, 12:53 PM
Mahmoud ayman
Mahmoud ayman - avatar