Printing a percent sign

Discussion in 'Python' started by stephen, Sep 25, 2006.

  1. stephen

    stephen Guest

    Hi all. How do I escape the "%" sign in a print statement so that it
    prints? Thanks.

    Stephen
     
    stephen, Sep 25, 2006
    #1
    1. Advertisements

  2. wrote in in
    comp.lang.python:
    print "%%"

    Rob.
     
    Rob Williscroft, Sep 25, 2006
    #2
    1. Advertisements

  3. stephen

    Georg Brandl Guest

    %

    Did you mean in a string being interpolated with the % operator?

    Georg
     
    Georg Brandl, Sep 25, 2006
    #3
  4. print doesn't do anything with percent signs:
    %

    if you're doing string formatting using the "string % tuple" operator,
    use two percent signs to get a percent sign in the output:
    level: 48%

    </F>
     
    Fredrik Lundh, Sep 25, 2006
    #4
  5. Rob Williscroft wrote in 216.196.109.145 in comp.lang.python:
    Ok, confused by the simplicity of the question.

    Real answer is:

    print "%"

    But the real question was "how to print a % whern doing % formating",

    acuracy = 100
    print "this is %d%% more acurate than my previous answer" % acuracy


    Rob.
     
    Rob Williscroft, Sep 25, 2006
    #5
  6. stephen

    stephen Guest

    Thanks -- a percent escapes itself when using %-formatting.

    Stephen
     
    stephen, Sep 25, 2006
    #6
  7. stephen

    John Machin Guest

    The following methods of getting answers to problems can be handy if
    it's non-peak hours on the net or your internet connection is
    broken/slow :

    1. Reasoning: How do you get a literal "'" into an SQL string constant?
    How do you get a literal "\" into a Python string constant? How do you
    get a literal "$" into some *x shell command lines? Do you detect a
    pattern?

    2. Inspecting the documentation: in this case, it says:
    """% <tab> No argument is converted, results in a "%" character in the
    result. """
    If that is not sufficiently clear, can you suggest how it might be
    improved?

    HTH generally,
    John
     
    John Machin, Sep 26, 2006
    #7
  8. None of which applies to escaping of % characters in format strings.
     
    Lawrence D'Oliveiro, Sep 26, 2006
    #8
  9. Its the pattern of escaping here, and yes, it applies: usually, a escaping
    character can be literally inserted by doubling it. I'm currently a bit
    unsure of the single-quote for sql though, but I'm oscillating between ''
    or '''. So - it applies.

    Diez
     
    Diez B. Roggisch, Sep 26, 2006
    #9
  10. stephen

    John Machin Guest

    What I had in mind was:

    where surname = 'O''REILLY'
    install_dir = "C:\\Python25"
    ....
    print "The interest rate is %.2f%% p.a." % (rate * 100.0)

    the common pattern being that the problem character is doubled.
     
    John Machin, Sep 26, 2006
    #10
  11. Which doesn't apply to the "$" character in *nix shell command lines.
     
    Lawrence D'Oliveiro, Sep 26, 2006
    #11
  12. stephen

    John Machin Guest

    I'll take your word for it; it's been quite a while :) *Something* in
    the dim dark past worked like that; I thought maybe I was thinking of
    m4, but that gets by without doubling.

    Your score so far is 1 out of 3; you have two more to go to match your
    original assertion "None of which applies...."

    Cheers,
    John
     
    John Machin, Sep 26, 2006
    #12
  13. makefiles?

    </F>
     
    Fredrik Lundh, Sep 26, 2006
    #13
  14. stephen

    John Machin Guest

    Bingo! Actually, double bingo!!
    """
    Because dollar signs are used to start make variable references, if you
    really want a dollar sign in a target or prerequisite you must write
    two of them, `$$' (see How to Use Variables). If you have enabled
    secondary expansion (see Secondary Expansion) and you want a literal
    dollar sign in the prerequisites lise [sic], you must actually write
    four dollar signs (`$$$$').
    """

    Cheers,
    John
     
    John Machin, Sep 26, 2006
    #14
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.