Oct-24-2019, 06:46 PM
Hi!
If I run this:
1) How to separate the multiline string by the new line escape ('\n')?
2) Why the error does say 'must be str or None, not set'? (Supposedly string6 is a string (string6 = '\n') and not a set).
Thank you and all the best,
If I run this:
string5 = '''Dear Alice, How have you been? I am fine. There is a container in the fridge that is labeled "Milk Experiment". Please do not drink it. Sincerely, Bob''' string6 = '\n' print(f"Applying string5.split('a') to 'string5' results in: {string5.split('a')}")it works and produces this output:
Output:Applying string5.split('a') to 'string5' results in: ['De', 'r Alice,\nHow h', 've you been? I ', 'm fine.\nThere is ', ' cont', 'iner in the fridge\nth', 't is l', 'beled "Milk Experiment".\nPle', 'se do not drink it.\nSincerely,\nBob']
But it seems that f-strings and backslashes don't like each other, so:string5 = '''Dear Alice, How have you been? I am fine. There is a container in the fridge that is labeled "Milk Experiment". Please do not drink it. Sincerely, Bob''' string6 = '\n' print(f"Applying string5.split('\n') to 'string5' results in: {string5.split('\n')}")produces the following:
Error:SyntaxError: f-string expression part cannot include a backslash.
I read somewhere that creating a string with the value of '\n' was a trick to avoid that error. So I tried:string5 = '''Dear Alice, How have you been? I am fine. There is a container in the fridge that is labeled "Milk Experiment". Please do not drink it. Sincerely, Bob''' string6 = '\n' print(f"Applying string5.split({string6}) to 'string5' results in: {string5.split({string6})}")but it doesn't do the trick and it shows the following error message:
Error:Traceback (most recent call last):
File "C:/Users/User1/AppData/Local/Programs/Python/Python37/atbs_06_checkingStrings_04.py", line 10, in <module>
print(f"Applying string5.split({string6}) to 'string5' results in: {string5.split({string6})}")
TypeError: must be str or None, not set
So now I have 2 doubts:1) How to separate the multiline string by the new line escape ('\n')?
2) Why the error does say 'must be str or None, not set'? (Supposedly string6 is a string (string6 = '\n') and not a set).
Thank you and all the best,
newbieAuggie2019
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs