Reverse String Without Changing Place of Special Characters

Abdulla Ansari
1 min readDec 3, 2022

Hi Programmer,

Today we are going to solve a string problem in python. Here you will get a string containing numbers and some special characters. We have to reverse this string without changing the place of the special characters.

*Input: * “123$456%789*0^”

*Output: * “098$765%432*1^”

So let’s start our coding

Solution: 1

seq = "123$456%789*0^"
output = "098$765%432*1^"

chars = ['$', '%', '*', '#', '^']
nums = []

for i in range(len(seq)):
if seq[i] not in chars:
nums.append(seq[i])

nums.reverse()

for j in seq:
if j in chars:
idx = seq.index(j)
nums.insert(idx, j)

reverse = "".join(nums)

print(reverse)

Solution: 2

ip_str = "123$456%789*0^"

rev_str = reversed(ip_str)
op_str = ""
for value in ip_str:
if value.isdigit():
for rev_val in rev_str:
if rev_val.isdigit():
op_str += rev_val
break
else:
op_str += value
print(op_str)

Here there were two types we can solve this problem. There can be more types also if you find anything, let’s discuss it in the room.

Buy me a coffee

My other Blog Website over Technology

My YouTube Vlog Channel

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Abdulla Ansari
Abdulla Ansari

Written by Abdulla Ansari

Connect with me to enhance yourself from a college guy to a senior software engineer in tech like python, javascript and now generative ai

No responses yet

Write a response