Printing a percent sign

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • stephen@theboulets.net

    Printing a percent sign

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

    Stephen

  • Rob Williscroft

    #2
    Re: Printing a percent sign

    wrote in news:1159216705 .649649.116260@ d34g2000cwd.goo glegroups.com in
    comp.lang.pytho n:
    Hi all. How do I escape the "%" sign in a print statement so that it
    prints? Thanks.
    >
    print "%%"

    Rob.
    --

    Comment

    • Georg Brandl

      #3
      Re: Printing a percent sign

      stephen@theboul ets.net wrote:
      Hi all. How do I escape the "%" sign in a print statement so that it
      prints? Thanks.
      >>print "%"
      %

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

      Georg

      Comment

      • Rob Williscroft

        #4
        Re: Printing a percent sign

        Rob Williscroft wrote in news:Xns9849DC7 DB4102rtwfreene tREMOVEcouk@
        216.196.109.145 in comp.lang.pytho n:
        wrote in news:1159216705 .649649.116260@ d34g2000cwd.goo glegroups.com in
        comp.lang.pytho n:
        >
        >Hi all. How do I escape the "%" sign in a print statement so that it
        >prints? Thanks.
        >>
        >
        print "%%"
        >
        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.
        --

        Comment

        • Fredrik Lundh

          #5
          Re: Printing a percent sign

          stephen@theboul ets.net wrote:
          Hi all. How do I escape the "%" sign in a print statement so that it
          prints? Thanks.
          print doesn't do anything with percent signs:
          >>print "%"
          %

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

          </F>

          Comment

          • stephen@theboulets.net

            #6
            Re: Printing a percent sign

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

            Stephen

            step...@theboul ets.net wrote:
            Hi all. How do I escape the "%" sign in a print statement so that it
            prints? Thanks.
            >
            Stephen

            Comment

            • John Machin

              #7
              Re: Printing a percent sign


              stephen@theboul ets.net wrote:
              Thanks -- a percent escapes itself when using %-formatting.
              >
              Stephen
              >
              step...@theboul ets.net wrote:
              Hi all. How do I escape the "%" sign in a print statement so that it
              prints? Thanks.
              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:
              """% <tabNo 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

              Comment

              • Lawrence D'Oliveiro

                #8
                Re: Printing a percent sign

                In message <1159230293.270 822.50390@d34g2 000cwd.googlegr oups.com>, John
                Machin wrote:
                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?
                None of which applies to escaping of % characters in format strings.

                Comment

                • Diez B. Roggisch

                  #9
                  Re: Printing a percent sign

                  Lawrence D'Oliveiro wrote:
                  In message <1159230293.270 822.50390@d34g2 000cwd.googlegr oups.com>, John
                  Machin wrote:
                  >
                  >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?
                  >
                  None of which applies to escaping of % characters in format strings.
                  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

                  Comment

                  • John Machin

                    #10
                    Re: Printing a percent sign


                    Lawrence D'Oliveiro wrote:
                    In message <1159230293.270 822.50390@d34g2 000cwd.googlegr oups.com>, John
                    Machin wrote:
                    >
                    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?
                    >
                    None of which applies to escaping of % characters in format strings.
                    What I had in mind was:

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

                    the common pattern being that the problem character is doubled.

                    Comment

                    • Lawrence D'Oliveiro

                      #11
                      Re: Printing a percent sign

                      In message <1159263309.699 677.301130@m7g2 000cwm.googlegr oups.com>, John
                      Machin wrote:
                      Lawrence D'Oliveiro wrote:
                      >In message <1159230293.270 822.50390@d34g2 000cwd.googlegr oups.com>, John
                      >Machin wrote:
                      >>
                      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?
                      >>
                      >None of which applies to escaping of % characters in format strings.
                      >
                      What I had in mind was:
                      >
                      where surname = 'O''REILLY'
                      install_dir = "C:\\Python 25"
                      ...
                      print "The interest rate is %.2f%% p.a." % (rate * 100.0)
                      >
                      the common pattern being that the problem character is doubled.
                      Which doesn't apply to the "$" character in *nix shell command lines.

                      Comment

                      • John Machin

                        #12
                        Re: Printing a percent sign


                        Lawrence D'Oliveiro wrote:
                        In message <1159263309.699 677.301130@m7g2 000cwm.googlegr oups.com>, John
                        Machin wrote:
                        >
                        Lawrence D'Oliveiro wrote:
                        In message <1159230293.270 822.50390@d34g2 000cwd.googlegr oups.com>, John
                        Machin wrote:
                        >
                        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?
                        >
                        None of which applies to escaping of % characters in format strings.
                        What I had in mind was:

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

                        the common pattern being that the problem character is doubled.
                        >
                        Which doesn't apply to the "$" character in *nix shell command lines.
                        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

                        Comment

                        • Fredrik Lundh

                          #13
                          Re: Printing a percent sign

                          John Machin wrote:
                          I'll take your word for it; it's been quite a while :-) *Something* in
                          the dim dark past worked like that
                          makefiles?

                          </F>

                          Comment

                          • John Machin

                            #14
                            Re: Printing a percent sign


                            Fredrik Lundh wrote:
                            John Machin wrote:
                            >
                            I'll take your word for it; it's been quite a while :-) *Something* in
                            the dim dark past worked like that
                            >
                            makefiles?
                            Bingo! Actually, double bingo!!
                            >From the docs for GNU Make:
                            """
                            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

                            Comment

                            Working...