Results 1 to 5 of 5

Thread: C# {n} like , in python

  1. #1
    Join Date
    Sep 2009
    Beans
    42

    C# {n} like , in python

    Hi , I've encountered a problem in python, as I've learned more advanced stuff and skipped the basics I'm missing a simple thing:
    I have 2 variables say x and y and I want to print:
    " x:value_of_x y:value_of_y"
    where the values are the real values of x and y
    in C# I would write:
    "x:{0} y:{1}",x,y
    can't find in py doc the appropriate syntax,
    but I think I once saw it's something like that "x:%d y:%d" %x %y
    but it doesn't work,it mumbles aboutאטפקד
    print '+%d*x^%d' %self.data[self.sed-i] %self.sed-i

    where sed is integer and the data[index] is integer THE ERROR:
    TypeError: not enough arguments for format string

    so if anybody could help me ,I would appreciate it.
    Thx in advance!!

    MCW

  2. #2
    Join Date
    May 2007
    Location
    I really do not know.
    Beans
    130
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: C# {n} like , in python

    you could just use string concatenation
    "X:"+str(x)+"Y:"+str(y)
    or that is called print formating
    %d stands for decimal %s for string there is also %h for hex and other stuff as well
    the way it works in python is
    "this is %s and you have %d lives" % ("sparta",98)
    you present it with a bunch of % and give it a tuple argument of all required replacements
    I think you must have printf in C# were you do
    printf("This is %s and you have %d lives %s","sparta",45,"man")
    The road not yet taken.

  3. #3
    Join Date
    Sep 2009
    Beans
    42

    Re: C# {n} like , in python

    Quote Originally Posted by 0cton View Post
    you could just use string concatenation
    "X:"+str(x)+"Y:"+str(y)
    or that is called print formating
    %d stands for decimal %s for string there is also %h for hex and other stuff as well
    the way it works in python is
    "this is %s and you have %d lives" % ("sparta",98)
    you present it with a bunch of % and give it a tuple argument of all required replacements
    I think you must have printf in C# were you do
    printf("This is %s and you have %d lives %s","sparta",45,"man&quot
    Thank you

  4. #4
    Join Date
    Jan 2007
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: C# {n} like , in python

    The % is deprecated in version 3. If you use v2.6 it is recommended to use the new syntax. http://docs.python.org/library/strin...#formatstrings

  5. #5
    Join Date
    Mar 2005
    Beans
    947
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: C# {n} like , in python

    Simplest, in this case (for Python 2.x):
    Code:
    print 'x:', x, 'y:', y
    or -- one string with formatting codes and a tuple of values:
    Code:
    print 'x: %d y: %d' % (x, y)
    2.6 and later add a new style that might seem familiar to you:
    Code:
    print 'x: {0} y: {1}'.format(x, y)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •