w3resource

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 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:

Flowchart: Add two strings as they are numbers (Positive integer values).

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:

Flowchart: Replace a word with hash characters in a string.

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.



Share this Tutorial / Exercise on : Facebook and Twitter

Python: Tips of the Day

Getting sorted iterable (can sort by "compare" function):

>>> a = [1, 2, -3]
>>> sorted(a)
[-3, 1, 2]

>>> sorted(a,key=abs)
[1, 2, -3]