2

I'm trying to make a string that includes a "%s" with no formatting

For example, I want this:

#I want to make mystring = "x %s"

first_value = "x"
mystring = "%s %s" % first_value

So, I want to format the first %s, but leave the second %s in the string.. Of course the above code gives me a "TypeError:not enough arguments for format string"

Is there a way to achieve this in one line?

Thanks


EDIT: I know I can do

mystring = "%s %s" % (first_value, "%s")

but this kind of looks ugly.. Is there a better way?

user3878106
  • 53
  • 1
  • 1
  • 8
  • Use `%%s` `>>> x = 'asdad' >>> mystring = "%%s %s" % x >>> mystring '%s asdad' ` – linuxfan Oct 15 '14 at 23:40
  • possible duplicate of [selectively escape percent (%) in python](http://stackoverflow.com/questions/10678229/selectively-escape-percent-in-python) – skrrgwasme Oct 15 '14 at 23:49

5 Answers5

13

You can escape a % character in a format string by doubling it:

>>> "%s %%s" % "x"
"x %s"
Blckknght
  • 100,903
  • 11
  • 120
  • 169
3

You can assign your string like this:

mystring = "{} %s".format(first_value)

If you are working with Python 2.6 or earlier, you will need to add an integer index in the curly braces:

mystring = "{0} %s".format(first_value)

A nice thing about this method is that first_value can change types later and you don't have to adjust your definition of mystring because .format() can take different types, and you don't need first_value's type to match a format string.

I also think it looks cleaner than escaping percent signs, but I know that is very subjective.

skrrgwasme
  • 9,358
  • 11
  • 54
  • 84
  • You can drop the integer index in python2.7 as well. AFAIK, python2.6 is the only version that supports `.format` where the index is required for positional arguments. – mgilson Oct 15 '14 at 23:53
  • 1
    @mgilson Wow. You are exactly right. My world just got turned upside down. I have been typing unnecessary characters in 2.7 for FAR TOO LONG to not be ashamed of it. – skrrgwasme Oct 15 '14 at 23:56
  • FWIW, I like the extra numbers in there. I find that it's more explicit and I like my code to be as backward compatible as possible if it's not terribly inconvenient. . . But I'm sure that others would probably consider that a pointless exercise . . . – mgilson Oct 16 '14 at 00:03
  • @mgilson I just hate it when I change the arguments being inserted into the string and then have to go back and shift all the indexes by one. I would much rather dump all the numbers so I only have to change the arguments to `.format()` and add another set of curly brackets. – skrrgwasme Oct 16 '14 at 00:06
  • 1
    Fair enough -- Although if you have that many, it's probably time to start naming them :-) `'{name} ...'.format(name='Bam Bam', strength='unbelievable')` – mgilson Oct 16 '14 at 00:08
0

Try concatenating instead:

mystring = ("%s " % first_value) + "%s"

outputs x %s

dave
  • 12,406
  • 10
  • 42
  • 59
0

Prepend %s with a % to escape it:

mystring = "%s %%s" % first_value
'x %s'
garnertb
  • 9,454
  • 36
  • 38
0

You can escape the first '%' by leading it with a second:

 print "%%s%s" % 'hello world'

%shello world

lstyls
  • 173
  • 2
  • 13