-1

I have a string that shows like that:

def function():
  my_string = """Balabala
  -
  Blala2: lala
  -
  Blala#End
  """

All the empty lines have that invisible character (character: ⠀).

When I print that string, I get:

Balabala
-Blala2: lala
-Blala#End

The - is an invisible character. Stackoverflow not allow that invisible character. What python should print:

Balabala
-
Balala2: lala
-
Blala#End

Does anyone have any advice for me or a solution?

EDIT: I use that string for Instagram Image description. Instagram not allow line breaks without a character. Because of that restriction, I searched for an invisible character. Now I want to implement that invisible character in my string.

Manuservus
  • 19
  • 1
  • 10
  • 1
    Your invisible character appears to be 'BRAILLE PATTERN BLANK', if it really is what you copy-pasted. If you want to remove it, you could replace it by an empty string: `my_string = my_string.replace('⠀', '')` – Thierry Lathuille Dec 06 '20 at 12:42
  • 1
    I cannot replace it or delete it. Because I need that pattern. – Manuservus Dec 06 '20 at 12:45
  • @Manuservus I tested the invisible character on my program; it works. – Red Dec 06 '20 at 12:47
  • 2
    So what is your question exactly? If you don't want to remove it, and you can't see it printed anyway, what is your problem? Please clarify... – Thierry Lathuille Dec 06 '20 at 12:48
  • I presume the problem is not Python, but the program to which Python prints does not show the 'secret character'. So we should instead direct the intention to that program and reason why it does not show this 'secret character'. – Hielke Walinga Dec 06 '20 at 12:54
  • @HielkeWalinga It's correct that the secret character not displays. I want to use that string for Instagram. In Instagram you cannot make line breaks without a point or a invisible character. I hope you now understand my question better. – Manuservus Dec 06 '20 at 13:00
  • @ThierryLathuille I did not clarify the question well. I edited the question and now I hope better to understand. – Manuservus Dec 06 '20 at 13:11
  • 1
    If your string contains the character you want, and it doesn't get printed as you expect, the problem is with your terminal. Next question is, does it matter for your use case? – Thierry Lathuille Dec 06 '20 at 13:17
  • @ThierryLathuille I solved the problem. – Manuservus Dec 06 '20 at 13:31
  • Are you aware that what `print` is showing is not the actual string, but a human readable (lossy) representation? Does the string fail in any way for your actual use case? – MisterMiyagi Dec 06 '20 at 13:31

1 Answers1

0

You can filter out the invisible characters with a list comprehension:

my_string = """Balabala

Blala2: lala

Blala#End
"""

my_string = ''.join(char for char in my_string if char.isascii())

print(my_string)

Output:

Balabala

Blala2: lala

Blala#End

Red
  • 26,798
  • 7
  • 36
  • 58