A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

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

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

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)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now