0

I'm trying to make a code that finds special characters and replace them in to *.
For example:

L!ve l@ugh l%ve

This should be changed as

L*ve l*ugh l*ve

Here it's what I tried so far

a = input()
spe = "+=/_(*&^%$#@!-.?)"
for i in a:
    if i in spe:
        b = a.replace(i,"*")
    else:
        b = i
        print(b,end="")

This returns something like this

Lve lugh lve

Why I'm getting like this?
And how to solve this issue?

1
  • 1
    Problem lies with the assignment of the variable 'b'. Each time it is being assigned a new value which changes in the next iteration. Since you don't display the value when you replace it by '*', it gets skipped over and the next assigned value is displayed in 'else' statement. Assuming you are just trying to achieve that output, I would suggest you to give a print statement in the 'if' statement as well. Commented Mar 6, 2022 at 17:06

5 Answers 5

2

You're trying to modify the whole string whereas you should only work on the character.

Modifying your code, this would be:

a = '(L!ve l@ugh l%ve)'
spe = set("+=/_(*&^%$#@!-.?)") # using a set for efficiency

for char in a:
    if char in spe:
        print('*', end='')
    else:
        print(char, end='')

output: *L*ve l*ugh l*ve*

A more pythonic way would be:

spe = set("+=/_(*&^%$#@!-.?)")
print(''.join(['*' if c in spe else c  for c in a]))
Sign up to request clarification or add additional context in comments.

1 Comment

Love list compression! The most pythonic way!
2

When you reach the if statement and if meets the condition it goes into the branch and executes. If all you want to do is print the statement you could run:

a = input()
spe = "+=/_(*&^%$#@!-.?)"
for i in a:
    if i in spe:
        b = "*"
        print(b)
    else:
        b = i
        print(b,end="")

but you could also save it as a string

a = input()
new_string = ""
spe = "+=/_(*&^%$#@!-.?)"
for i in a:
    if i in spe:
        new_string += "*"
    else:
        new_string += i
print(new_string)

Comments

1

A simple way to do it:

import string

all_chars = string.ascii_letters

a = 'L!ve l@ugh l%ve'

for item in a:
    if item ==' ':
        pass
    elif item not in all_chars:
        item='*'
        
    print(item, end="")

Comments

1

As another alternative, you could try a regular expression. Here are two potential approaches depending on how you want to define your set of characters:

import re
print(re.sub('[^a-zA-Z\d\s]', '*', "L!ve l@ugh l%ve"))
print(re.sub("[$&+,:;=?@#|'<>.^*()%!-]", '*', "L!ve l@ugh l%ve"))
# L*ve l*ugh l*ve

Comments

0

There are two problems in your script:

  • if the special characters are found you replace them in b and not in I
  • you only print if the special characters are not found.
    Try:
a = "L!ve l@ugh l%ve"
spe = "+=/_(*&^%$#@!-.?)"
for i in a:
    if i in spe:
        b = i.replace(i,"*")
    else:
        b = i
    print(b,end="")

Or, as we replace the characters in the original string, we can print it at the end:

a = "L!ve l@ugh l%ve"
spe = "+=/_(*&^%$#@!-.?)"
for i in a:
    if i in spe:
        b = a.replace(i,"*")
    else:
        b = a
print(b)

You should also consider using regular expressions:

import re
a = "L!ve l@ugh l%ve"                     
print(re.sub("[+\=\/_\(*&^%\$#@\!-\.\?\)]","*",a))

Which has exactly the same output.

1 Comment

This seems like my solution. Maybe, we're missing something here?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.