Reputation: 88738
How should this function be changed to return "123456"
?
def f():
s = """123
456"""
return s
UPDATE: Everyone, the question is about understanding how to not have \t or whatever when having a multiline comment, not how to use the re
module.
Upvotes: 34
Views: 43624
Reputation: 6199
You might want to check this str.splitlines([keepends])
Return a list of the lines in the string, breaking at line boundaries. This method uses the universal newlines approach to splitting lines. Line breaks are not included in the resulting list unless keepends is given and true.
Python recognizes "\r", "\n", and "\r\n" as line boundaries for 8-bit strings.
So, for the problem at hand ... we could do somehting like this..
>>> s = """123
... 456"""
>>> s
'123\n456'
>>> ''.join(s.splitlines())
'123456'
Upvotes: 0
Reputation: 121
textwrap.dedent("""\
123
456""")
From the standard library. First "\" is necessary because this function works by removing the common leading whitespace.
Upvotes: 12
Reputation: 706
My guess is:
def f():
s = """123
456"""
return u'123456'
Minimum change and does what is asked for.
Upvotes: -3
Reputation: 223172
Don't use a triple-quoted string when you don't want extra whitespace, tabs and newlines.
Use implicit continuation, it's more elegant:
def f():
s = ('123'
'456')
return s
Upvotes: 55
Reputation: 33250
Subsequent strings are concatenated, so you can use:
def f():
s = ("123"
"456")
return s
This will allow you to keep indention as you like.
Upvotes: 16
Reputation: 239
def f():
s = """123\
456"""
return s
Don't indent any of the blockquote lines after the first line; end every line except the last with a backslash.
Upvotes: 21
Reputation: 320039
re.sub('\D+', '', s)
will return a string, if you want an integer, convert this string with int
.
Upvotes: -1
Reputation: 308269
Maybe I'm missing something obvious but what about this:
def f():
s = """123456"""
return s
or simply this:
def f():
s = "123456"
return s
or even simpler:
def f():
return "123456"
If that doesn't answer your question, then please clarify what the question is about.
Upvotes: 7