Q:

Replace each special symbol with # in the following string using python programming

belongs to collection: Python String Exercises

0

Replace each special symbol with # in the following string

Given:

str1 = '/*Jon is @developer & musician!!'

Expected Output:

##Jon is #developer # musician##

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Hint:

Use string function replace()

Solution:

  • Use the string.punctuation constant to get the list of all punctuations
  • Iterate each symbol from a punctuations
  • Use string function replace() to replace the current special symbol in a string with #
import string

str1 = '/*Jon is @developer & musician!!'
print("The original string is : ", str1)

# Replace punctuations with #
replace_char = '#'

# string.punctuation to get the list of all special symbols
for char in string.punctuation:
    str1 = str1.replace(char, replace_char)

print("The strings after replacement : ", str1)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python String Exercises

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
<< Write a python program to find words with both alp...