+ 1

How to convert a string to a docstring?

a='hello \ sir' b=f"\'''{a}\'''" print(b) Output: '''hello sir''' Expected Output: hello sir

3rd Mar 2021, 1:48 AM
Ujjawal Gupta
Ujjawal Gupta - avatar
1 Respuesta
+ 1
Skipping the fact that a docstring is actually so when it's bound to the __doc__ of an object, using """...""" is just sintattic sugar, at the end of the day it is a normal string. If you want to print: hello sir Your string has to be "hello\nsir" whether you write it directly or with the """...""" notation The \ at the end of a line just tells python that the line actually continues in the one below, so a='hello \ sir' Is actually a='hello sir', there is no way to recover the '\n' you wanted without corrupting the whole string Putting the triple quotes inside a string will not create a docstring, but just a string with a triple quote in it (but you could actually use eval to do that)
3rd Mar 2021, 2:28 AM
Angelo
Angelo - avatar