I am trying to print a string in Python 2.7 but the string contains the character "(150\xb5s)" and it cannot print x5bs and so it gives error for that position of the string.
UnicodeEncodeError: "ascii" codec can't encode character u'\xb5'
string_print = "29 days in (F150\xb5s)"
with open("testing_file.txt", "w") as fill:
fill.write(string_print)
I tried doing this from stackoverflow solution but still got same error
string_print = "29 days in (F150\xb5s)"
with open("testing_file.txt", "w") as fill:
fill.write(string_print.decode("utf-8"))
using a raw string method works but the problem is that string_print can have any string as I am just making a very long program short to better isolate the problem. So is there a way to make the variable string_print a part of raw string. If I just do raw string of "29 days in (F150\xb5s)" it works.
r"29 days in (F150\xb5s)"
But string_print can have any type of string