How do you escape % when formatting a string?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • erikcw

    #1

    How do you escape % when formatting a string?

    Hi,

    I'm trying to format a string like so:

    string = "You have a 75% chance of success with %s, don't use %s" %(a,
    b)

    This gives me:
    TypeError: not enough arguments for format string

    I've tried 75\%, but that doesn't seem to help. What am I missing?

    Thanks!
    Erik

  • irstas@gmail.com

    #2
    Re: How do you escape % when formatting a string?

    On Apr 2, 10:50 pm, "erikcw" <erikwickst...@ gmail.comwrote:
    Hi,
    >
    I'm trying to format a string like so:
    >
    string = "You have a 75% chance of success with %s, don't use %s" %(a,
    b)
    >
    This gives me:
    TypeError: not enough arguments for format string
    >
    I've tried 75\%, but that doesn't seem to help. What am I missing?
    >
    Thanks!
    Erik
    Use %%.
    >>"%d%%" % 75
    "75"


    Comment

    • irstas@gmail.com

      #3
      Re: How do you escape % when formatting a string?

      On Apr 2, 10:52 pm, irs...@gmail.co m wrote:
      On Apr 2, 10:50 pm, "erikcw" <erikwickst...@ gmail.comwrote:
      >
      Hi,
      >
      I'm trying to format a string like so:
      >
      string = "You have a 75% chance of success with %s, don't use %s" %(a,
      b)
      >
      This gives me:
      TypeError: not enough arguments for format string
      >
      I've tried 75\%, but that doesn't seem to help. What am I missing?
      >
      Thanks!
      Erik
      >
      Use %%.
      >
      >"%d%%" % 75
      >
      "75"
      Oups, I made a typo in the result which should've been "75%"

      Comment

      • Terry Reedy

        #4
        Re: How do you escape % when formatting a string?


        "erikcw" <erikwickstrom@ gmail.comwrote in message
        news:1175543459 .131630.96650@y 80g2000hsf.goog legroups.com...
        | Hi,
        |
        | I'm trying to format a string like so:
        |
        | string = "You have a 75% chance of success with %s, don't use %s" %(a,
        | b)
        |
        | This gives me:
        | TypeError: not enough arguments for format string
        |
        | I've tried 75\%, but that doesn't seem to help. What am I missing?

        The reference manual?



        Comment

        Working...