10

Replace all characters in string with asterisks – Python

 2 years ago
source link: https://thispointer.com/replace-all-characters-in-string-with-asterisks-python/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Replace all characters in string with asterisks – Python

In this article, we will discuss different ways to replace all characters in a string with asterisks i.e. “*”.

Table Of Contents

Suppose we have a string like this,

"Testing"
"Testing"

After replacing all characters in this string with asterisks, the final string should be like,

"*******"
"*******"

Number of asterisks in the final string should be same as the number of characters in the original string. There are different ways to do this. Let’s discuss them one by one.

Advertisements

vid5e6258f9da92c874459691.jpg?cbuster=1600267117
00:00/11:46
liveView.php?hash=ozcmPTEznXRiPTEzqzyxX2V2ZW50PTUjJaNypaZypyRcoWU9MTY1MTU5NDYmMvZ2nWRspGkurWVlVzVlPTMhMS4jJaM9MTAkMwx3JaN0YT0jJat9NDUmJax9MmI1JaZcZF9jYXNmRG9gYWyhPXRbnXNjo2yhqGVlLzNioSZmqWJJZD10nGympG9coaRypv5wo20zZGVvqWqJozZipz1uqGyiow0znXNBpHA9MCZlnT02QmY5NmY2NTUmNmQ2MTp0NmM3QmpmNxImMTqCNTQmMDqEN0I2NDMlMmAmMwMlMxQmMDM1MxQmMDMmNUYmMTM5N0Q3QwpmMmEmMwMmMmQmOTM2MmQmOTqEN0I0MmMkMmpmMwqEN0I1MmY0NDp2ODpjNwMmMmQlNmY2MTU3MmUmMDVBNTt0OTp1NTxmMwM5NmQ3RDqCNwI2MmY4NmI2RwZENwU3RDqCNmE2NDY1NmM2Qwp0NxY3MDqEN0I2RwZDNwx2RTp1Nmt3RDqCNTtmNDM1MmM3RDqCNTxmMmMlMmU3RDqCNwYmMTqEN0I0QmMkMmImNTMlMmE3REZFRxUzZGyunWQ9JaVmZXJJpEFxZHI9MTQkLwE2NC42Ml4kNwQzqXNypyVBPU1irzyfoGEyMxY1LwAyMwAyMwuYMTEyM0IyMwBMnW51rCUlMHt4Ny82NCUlOSUlMEFjpGkyV2VvS2y0JTJGNT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qEyxPTAzoWVxnWFMnXN0SWQ9MCZxqXI9ODAkJzqxpHI9MCZaZHBlQ29hp2VhqD0znXNXZVBup3NHZHBlPTEzY2NjYT0jJzNwpGFDo25mZW50PSZwYaVmqGVlPTE2NTE1OTQ2NDI5MTIzqWyxPVNyn2yhZG9TUGkurWVlNwI3MTU1ODt5OTt0ZCZjqWJVpzj9nHR0pHMyM0EyMxYyMxZ0nGympG9coaRypv5wo20yMxZlZXBfYWNyLWFfoC1wnGFlYWN0ZXJmLWyhLXN0pzyhZl13nXRbLWFmqGVlnXNepl1jrXRbo24yMxYzZzkiYXRTqGF0qXM9ZzFfp2UzZWyxp3A9pHJyYzyx

Using len() function

As we need to replace all the characters in a string with an asterisk ‘*’ symbol. So, we can just count the number of characters in the string and create a new string what that many asterisks. Then assign this new string to the original variable. It will give an effect that we have replaced all the characters in the string with asterisks.

For example,

strValue = "Testing"
# Replace all characters in a string with asterisks
strValue = '*' * len(strValue)
print(strValue)
strValue = "Testing"

# Replace all characters in a string with asterisks
strValue = '*' * len(strValue)

print(strValue)

Output:

*******
*******

It replaced all the characters in the string with asterisks i.e. ‘*’.

By converting into list

As strings are immutable in Python. Therefore, we can not modify its contents using [] operator. But we can convert a string to list and then change its contents using the subscript operator ([]). Now to replace all characters in string with asterisks, we can use the this technique i.e.

  • Convert the string to list.
  • Iterate over list and replace each value with ‘*’ using subscript operator.
  • Join all characters in list and create a string again.
  • This string will contain only asterisks.
  • Assign this string back to the original variable.

It will give an effect that we have replaced all the characters in string with asterisks.

For example,

strValue = "Testing"
# Convert string to list
listOfChars = list(strValue)
# Iterate over all characters in list
# and replace them with asterisks.
for i in range(len(strValue)):
listOfChars[i] = '*'
# Convert list to string, containing all asterisks
strValue = ''.join(listOfChars)
print(strValue)
strValue = "Testing"

# Convert string to list
listOfChars = list(strValue)

# Iterate over all characters in list
# and replace them with asterisks.
for i in range(len(strValue)):
    listOfChars[i] = '*'

# Convert list to string, containing all asterisks 
strValue = ''.join(listOfChars)

print(strValue)

Output:

*******
*******

It replaced all the characters in the string with asterisks i.e. ‘*’.

Summary:

We learned about different ways to replace all the characters in a string with asterisks in Python.

Pandas Tutorials -Learn Data Analysis with Python

 

 

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK