string formatting.

34 views
Skip to first unread message

jettam

unread,
Oct 11, 2017, 8:11:54 PM10/11/17
to Python Programming for Autodesk Maya
Can someone help me with this string formatting. 

def sayHello (name=None, age=None, weight=None ):
   
print( "name: {0} age:{1} weight:{2}".format(sayHello)

sayHello
("Allen", 20, 150)


Simon Anderson

unread,
Oct 11, 2017, 8:19:52 PM10/11/17
to Python Programming for Autodesk Maya
you need to assign the variables into the format method.

def
 sayHello (name=None, age=None, weight=None ):

    
print( "name: {0} age:{1} weight:{2}".format(name, age, weight)


sayHello 
("Allen", 20, 150)

jettam

unread,
Oct 11, 2017, 8:23:40 PM10/11/17
to Python Programming for Autodesk Maya
I get an error when I try that.
# Error: invalid syntax
#   File "<maya console>", line 4
#     print( "name: {0} age:{1} weight:{2}".format(name, age, weight)
#                                                                   ^
# SyntaxError: invalid syntax # 

Simon Anderson

unread,
Oct 11, 2017, 8:31:56 PM10/11/17
to Python Programming for Autodesk Maya
missing the last close bracket

print( "name: {0} age:{1} weight:{2}".format(sayHello))

outputString = "name: {0} age:{1} weight:{2}".format(sayHello)
print( outputString )


On Thursday, 12 October 2017 11:11:54 UTC+11, jettam wrote:

damon shelton

unread,
Oct 11, 2017, 8:50:02 PM10/11/17
to python_in...@googlegroups.com
I would suggest utilizing keywords rather than order - also a nice trick is expanding locals to keep from having to specify the keywords repeatedly
def sayHello(name=None, age=None, weight=None ):
    print( "name: {name} age:{age} weight:{weight}").format(**locals())

sayHello("Allen", 20, 150)



--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/d18200a5-3d0d-4497-9aab-0eb86fe6e268%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

damon shelton

unread,
Oct 11, 2017, 8:54:07 PM10/11/17
to python_in...@googlegroups.com
sorry fixed a typo - didn't stop the code from working but wanted to make sure it was properdef sayHello(name=None, age=None, weight=None ):
    print( "name: {name} age:{age} weight:{weight}".format(**locals()))

sayHello("Allen", 20, 150)

Simon Anderson

unread,
Oct 11, 2017, 8:56:30 PM10/11/17
to Python Programming for Autodesk Maya
+1 :)

Robert White

unread,
Oct 11, 2017, 9:17:20 PM10/11/17
to Python Programming for Autodesk Maya
Reply all
Reply to author
Forward
0 new messages