Python Exercises: Replace a word with hash characters in a string
Python String: Exercise-103 with Solution
Write a Python program to replace each character of a word of length five and more with a hash character (#).
Sample Solution-1:
Python Code:
def test(text):
for i in text.split():
if len(i) >= 5:
text = text.replace(i, "#" * len(i))
return text
text ="Count the lowercase letters in the said list of words:"
print("Original string:", text)
print("Replace words (length five or more) with hash characters in the said string:")
print(test(text))
text = "Python - Remove punctuations from a string:"
print("\nOriginal string:", text)
print("Replace words (length five or more) with hash characters in the said string:")
print(test(text))
Sample Output:
Original string: Count the lowercase letters in the said list of words: Replace words (length five or more) with hash characters in the said string: ##### the ######### ####### in the said list of ###### Original string: Python - Remove punctuations from a string: Replace words (length five or more) with hash characters in the said string: ###### - ###### ############ from a #######
Flowchart:

Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Sample Solution-2:
Python Code:
def test(text):
return ' '.join('#' * len(i) if len(i) >= 5 else i for i in text.split(' '))
text ="Count the lowercase letters in the said list of words:"
print("Original string:", text)
print("Replace words (length five or more) with hash characters in the said string:")
print(test(text))
text = "Python - Remove punctuations from a string:"
print("\nOriginal string:", text)
print("Replace words (length five or more) with hash characters in the said string:")
print(test(text))
Sample Output:
Original string: Count the lowercase letters in the said list of words: Replace words (length five or more) with hash characters in the said string: ##### the ######### ####### in the said list of ###### Original string: Python - Remove punctuations from a string: Replace words (length five or more) with hash characters in the said string: ###### - ###### ############ from a #######
Flowchart:

Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous Python Exercise: Remove punctuations from a string.
Next Python Exercise: Capitalize the first letter and lowercases the rest.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Enum Class:
Here is another classy approach from Python, no pun intended. Python's enum class will let you create enum object members with constant and unique values which then can be listed, compared or used for identification.
Enum classes are also iterable so they can be iterated as well:
from enum import Enum class sports(Enum): volleyball = 1 soccer = 2 running = 3 football = 4 baseball = 5 boxing = 6 print(sports.baseball) print(repr(sports.baseball))
Output:
sports.baseball <sports.baseball: 5>
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises