print and % operator

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

    #1

    print and % operator

    Hi,

    I am new to Python, so this may be a very simple question for most of
    you. What does the % operator stand for in Python? I came across a
    script that uses the % operator as follows -

    def sync(delf,name) :
    os.popen3( 'P4 -s sync -f %s' % name)

    I would suspect that this just replaces the %s with the value of name.
    Is % before name required? Should there be a space between % and name?

    On a similar note, how can I print the value of name using the print
    statement? Should print %name print the value of name?

    Most of the time, when I add some print statements to my script I get
    Autoindent error. Can someone tell me what this error is and how to
    fix it?

    Thanks for your help.

    Ruchika
  • Aaron Bingham

    #2
    Re: print and % operator

    Ruchika wrote:
    [color=blue]
    > I am new to Python, so this may be a very simple question for most of
    > you. What does the % operator stand for in Python?[/color]



    --
    --------------------------------------------------------------------
    Aaron Bingham
    Application Developer
    Cenix BioScience GmbH
    --------------------------------------------------------------------

    Comment

    • Alex Martelli

      #3
      Re: print and % operator

      Ruchika <ruchika_khera@ hotmail.com> wrote:
      [color=blue]
      > Hi,
      >
      > I am new to Python, so this may be a very simple question for most of
      > you. What does the % operator stand for in Python? I came across a[/color]

      Between numbers, it means "remainder of division", aka "modulo"; if the
      left-hand operator is a string, it means "formatting ".
      [color=blue]
      > script that uses the % operator as follows -
      >
      > def sync(delf,name) :
      > os.popen3( 'P4 -s sync -f %s' % name)[/color]

      Here it's formatting, as the LHO is a string,
      [color=blue]
      >
      > I would suspect that this just replaces the %s with the value of name.
      > Is % before name required? Should there be a space between % and name?[/color]

      The % is indeed required, spaces are optional (like most always between
      operators and other tokens -- no difference there between the operator %
      and other operators such as + * and so on).

      [color=blue]
      > On a similar note, how can I print the value of name using the print
      > statement? Should print %name print the value of name?[/color]

      No, there is no unary version of % -- just use:

      print name
      [color=blue]
      >
      > Most of the time, when I add some print statements to my script I get
      > Autoindent error. Can someone tell me what this error is and how to
      > fix it?[/color]

      Sounds like a problem with your editor, not with Python.


      Alex

      Comment

      • Josef Meile

        #4
        Re: print and % operator

        Hi Ruchika,
        [color=blue]
        > I am new to Python, so this may be a very simple question for most of
        > you. What does the % operator stand for in Python?[/color]
        It is an operator for formating strings. Ie: lets say you want to print
        a string followed by an integer value embebbed on a string, then you
        could do:

        myStr='Test'
        value=10
        print "String: %s value: %i" % (myStr, value)

        This will print:
        String: Test value: 10

        You could do the same by concatenating the strings:
        print "String: " + myStr + "value: " + str(value)

        However, the first piece of code is easier to understand and faster.

        This link may help:


        [color=blue]
        >I came across a
        > script that uses the % operator as follows -
        >
        > def sync(delf,name) :
        > os.popen3( 'P4 -s sync -f %s' % name)
        >
        > I would suspect that this just replaces the %s with the value of name.[/color]
        That's right.
        [color=blue]
        > Is % before name required?[/color]
        Yes, % is required since you included "%s" on the string, so, you have
        to match it with some variable/constant. If you use "%i" instead, then
        you will have to use an int variable/constant.
        [color=blue]
        >Should there be a space between % and name?[/color]
        No, you could use:
        os.popen3( 'P4 -s sync -f %s'%name)

        but it looks ugly and it's difficult to read.
        [color=blue]
        > On a similar note, how can I print the value of name using the print
        > statement? Should print %name print the value of name?[/color]
        No, if you want to print a variable directly you should use:
        print name

        if you want to print it inside a string then you should do:
        print "This is the value of name: %s" % name
        [color=blue]
        >
        > Most of the time, when I add some print statements to my script I get
        > Autoindent error. Can someone tell me what this error is and how to
        > fix it?[/color]
        Indentation may be wrong, check if you are using the same number of
        tabs/spaces on each indentation level.

        Regards,
        Josef

        Comment

        • Peter Hansen

          #5
          Re: print and % operator

          Ruchika wrote:[color=blue]
          > I am new to Python, so this may be a very simple question for most of
          > you. What does the % operator stand for in Python? I came across a
          > script that uses the % operator as follows -
          >
          > def sync(delf,name) :
          > os.popen3( 'P4 -s sync -f %s' % name)
          >
          > I would suspect that this just replaces the %s with the value of name.
          > Is % before name required? Should there be a space between % and name?[/color]

          You _really_ need to go through the tutorial... it answers
          this question and others you have had and many others you
          will have.

          Also consider reading the main FAQ entries soon!

          -Peter

          Comment

          • Brian Elmegaard

            #6
            Re: print and % operator

            Peter Hansen <peter@engcorp. com> writes:
            [color=blue]
            > You _really_ need to go through the tutorial... it answers
            > this question[/color]

            Are you sure? I never found the answer to this. What I found was: "Most
            formats work exactly as in C and require that you pass the proper
            type; however, if you don't you get an exception, not a core dump."

            So you need to know C to use python's tutorial :-(

            Josef's link is great though.

            --
            Brian (remove the sport for mail)

            Comment

            • Peter Hansen

              #7
              Re: print and % operator

              Brian Elmegaard wrote:[color=blue]
              > Peter Hansen <peter@engcorp. com> writes:
              >
              >[color=green]
              >>You _really_ need to go through the tutorial... it answers
              >>this question[/color]
              >
              >
              > Are you sure? I never found the answer to this. What I found was: "Most
              > formats work exactly as in C and require that you pass the proper
              > type; however, if you don't you get an exception, not a core dump."[/color]

              Yes, I'm sure. http://docs.python.org/tut/node9.html

              -Peter

              Comment

              Working...