33

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.

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
Ram Rachum
  • 84,019
  • 84
  • 236
  • 374

9 Answers9

54

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
nosklo
  • 217,122
  • 57
  • 293
  • 297
  • 1
    This used to come naturally to me but not when I had to plug in variable values using a ex:%s , but alas, even the following works: `s = ('String %s '` `'String %s') %('foo', 'bar')` – Spade Dec 15 '14 at 08:41
18
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.

Slumberheart
  • 209
  • 2
  • 4
  • 1
    +1: even though I do not like bashslashes: could use replace('\n','') – van Oct 05 '09 at 17:52
  • ... and not {{{return s.replace('\n', '')}}}, but rather in the assignment itself: {{{456""".replace('\n', '')}}} – van Oct 05 '09 at 17:56
15

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.

Denis Otkidach
  • 32,032
  • 8
  • 79
  • 100
  • Note to OP - no comma between "123" and "456". The ()'s are there to avoid having to use '\' for line continuations. – PaulMcG Oct 05 '09 at 19:19
11
textwrap.dedent("""\
                123
                456""")

From the standard library. First "\" is necessary because this function works by removing the common leading whitespace.

claudelepoisson
  • 111
  • 1
  • 2
7

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.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • About understanding how to not have \t or whatever when have a multiline comment. – Ram Rachum Oct 05 '09 at 15:11
  • 9
    A triple quoted string isn't a multiline comment, it's a raw string, and contains exactly what you type into it. – JimB Oct 05 '09 at 15:20
  • 3
    Technically a triple-quoted string is not a raw string, as backslash escapes are still interpreted. It is a normal python string in which unescaped newlines are retained (instead of causing a parse error). You can make a truly raw triple-quoted string with r"""blabla""". This is useful for making regex patterns which need to contain both single and double quotes and backslashes. – pix Oct 10 '14 at 04:03
0

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'
Community
  • 1
  • 1
Rishi
  • 5,869
  • 7
  • 34
  • 45
-1
re.sub('\D+', '', s)

will return a string, if you want an integer, convert this string with int.

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
-1

Try

import re

and then

    return re.sub("\s+", "", s)
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
-3

My guess is:

def f():
    s = """123
    456"""
    return u'123456'

Minimum change and does what is asked for.

Juparave
  • 706
  • 5
  • 11
  • Was probably downvoted because the function was supposed to return a string. – recursive Oct 05 '09 at 17:28
  • You're right, it probably has to return a string and I returned an integer, anyway I don't get the downvoted thing either, I'm new to python – Juparave Oct 05 '09 at 17:47
  • so change the last line to return "123456" – foosion Oct 05 '09 at 18:12
  • Now that I re-read the question I know why my answer was downvoted, the question is referring to 'How does Python’s triple-quote string work?' an my answer doesn't comment anything about it – Juparave Oct 05 '09 at 18:43