How To Remove Zero Width Space Character From String in Python

Remove zero width space character from string in Python

In this article, I will introduce you to some ways to remove zero width space character from string in Python, like using the encode() and decode() function, removeprefix() function, replace() function… Please read the following article.

Remove zero width space character from string in Python 

Use the encode() and decode() function

Example:

  • Initialize the string with a space character with a width of 0.
  • The encode function encodes the string using ASCII encoding.
  • The errors argument is ‘ignore’, which ignores non-ASCII characters.
  • The decode function with ‘utf-8’ encoding decodes those ‘bytes’ objects.
# \u200c is a space character with a width of 0
myString = '\u200cVisit learnshareit'

# Use the encode() fucntion and the decode() function
myEncode = myString.encode('ascii', errors='ignore')
newString = myEncode.decode("utf-8")

print('String after removing character:', newString)

Output:

String after removing character: Visit learnshareit

Use the str.removeprefix function.=

You can use the str.remove prefix function. Note this method was only introduced in Python 3.9.

Syntax:

str.removeprefix(prefix)

Parameters:

  • prefix: the prefix string.

The str.removeprefix function returns the remainder of the string and removes the prefix. Otherwise, the original string is returned.

Example:

  • Initialize the string with a space character with a width of 0.
  •  Use the str.removeprefix function to remove zero width space character.
# \u200c is a space character with a width of 0
myString = '\u200cVisit learnshareit'

# Use the str.removeprefix function to remove zero width space character
newString = myString.removeprefix('\u200c')

print('String after removing character:', newString)

Output:

String after removing character: Visit learnshareit

Use the replace() function

Syntax:

str.replace(old, new, count)

Parameters:

  • old: Original string.
  • new: String will replace the original string.
  • count: Specify the maximum number of character substitutions.

Example:

  • Initialize the string with a space character with a width of 0.
  •  Use the replace function to remove zero width space character .
# \u200c is a space character with a width of 0
myString = '\u200cVisit learnshareit'

# Use the replace function
newString = myString.replace('\u200c', '')

print('String after removing character:', newString)

Output:

String after removing character: Visit learnshareit

Get a slice excluding zero width space characters

Example:

  • Initialize the string with a space character with a width of 0.
  • Perform slice on the string to remove zero width space character.
# \u200c is a space character with a width of 0
myString = '\u200cVisit learnshareit'

# Perform cut in string. Remove zero width space character
newString = myString[1:]

print('String after removing character:', newString)

Output:

String after removing character: Visit learnshareit

Use the str.lstrip function

Syntax:

str.lstrip([chars])

Parameters:

  • chars: The character to be cut.

The str.lstrip function returns a copy of the original string when truncating the character to be cut has been removed.

Example:

  • Initialize the string with a space character with a width of 0.
  • Use the str.lstrip to remove zero width space character .
# \u200c is a space character with a width of 0
myString = '\u200cVisit learnshareit'

# Use the str.lstrip to remove zero width space character
newString = myString.lstrip('\u200c')

print('String after removing character:', newString)

Output:

String after removing character: Visit learnshareit

Summary

Thank you for taking the time to read my post. Hope you got an idea for the topic of how to remove zero width space character from string in Python. Wish you success with it. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *