using .format

60 views
Skip to first unread message

s...@weacceptyou.com

unread,
Jun 5, 2016, 3:00:05 PM6/5/16
to Python Programming for Autodesk Maya
hey,

i understand using .format i can substitute in a value into the middle of string

eg.

word = 'cat'
print 'the {} sat on the mat'.format(word)

but what if you want to substitute in a string into a line which has other operations at the end. Like this example

cmds.getAttr(curves['curve{}'].spans*2+1

here i am accessing a curve from a dictionary and finding its span value and then multiplying by 2 + 1.

So where in this line can i put the .format(value) ?. I cant put it right at the end or put it right after the string in between the string and the ].

can someone help me understand how i can do this. Using the concatenation + doesnt work either because is just returns:

unsupported operand type(s) for +: 'NoneType' and 'str'

any help on this would be nice,
thanks,
Sam

Justin Israel

unread,
Jun 5, 2016, 3:22:43 PM6/5/16
to Python Programming for Autodesk Maya

It always goes after the string, no matter what. It is a method of a string

cmds.getAttr(curves['curve{}'.format('xyz')].spans*2+1

But you can avoid hard to read lines and break it apart

curve = curves['curve{}'.format('xyz')]
spans = cmds.getAttr('{}.spans'.format(curve))
spans = spans * 2 + 1


--
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_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/9d99b0ee-88f3-4a44-9263-0bf8012c1a4b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

s...@weacceptyou.com

unread,
Jun 5, 2016, 4:57:05 PM6/5/16
to Python Programming for Autodesk Maya
thanks alot Justin

Sam

s...@weacceptyou.com

unread,
Jun 5, 2016, 6:16:58 PM6/5/16
to Python Programming for Autodesk Maya, s...@weacceptyou.com
one last thing,

i am currently splitting things up like you showed which works. But is there actually a way to fit this all on one line if there are two string chunks seperated?.

the problem is after the 'curve{}' there needs to be the other square brace ] then followed by the second string'.spans'

cmds.getAttr(curves['curve{}'.format('xyz')].spans)*2+1

should i just use a + inbetween ] and '.spans'

thanks,
Sam

Justin Israel

unread,
Jun 5, 2016, 6:29:11 PM6/5/16
to python_in...@googlegroups.com, s...@weacceptyou.com
There is usually a way to do it all on one line, if that is your goal and you find it readable:

cmds.getAttr("{}.spans".format(curves['curve{}'.format('xyz')]))*2+1

Do you find this readable?

Justin
 

thanks,
Sam

--
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_m...@googlegroups.com.

Geordie Martinez

unread,
Jun 7, 2016, 1:34:08 AM6/7/16
to python_inside_maya, s...@weacceptyou.com

Hey Sam,
attached is a cheat sheet I made for string formatting with .format() for my workshop. it might help you with your questions.

also

I do have a whole section on string formatting in my python workshop that I’m starting again at the end of June.
if you’re interested take a look:

http://www.cgcircuit.com/workshops/1


stringFormattingCheatSheet_v002.pdf

s...@weacceptyou.com

unread,
Jun 7, 2016, 3:31:17 PM6/7/16
to Python Programming for Autodesk Maya, s...@weacceptyou.com
thanks this is very handy

cheers,
Sam
Reply all
Reply to author
Forward
0 new messages