Intro to Python Scripting: 11 Strings and Lists

Video Duration: 
11 minutes
Zach Downey

We take a quick step back in this tutorial to cover a couple of basic string and list methods. These methods are extremely handy when we start building things up in our Python scripts, in fact, we use a couple of them immediately in the next tutorial. You can find out a bit more about strings here and lists here. These are pretty good resources for basic operations. I can't cover everything so make sure you take a look at as many tutorials as you can. Keep in mind though, that Python for Rhinoscript isIronPython NOT Python. There is a difference. Most tutorials out there are for Python 2.x or Python3.x, not IronPython. Most functions will still work, you might find subtle differences. Sometimes the best way to learn is to try and fail. I do it all the time. It's no big deal and it's a great way to learn.

#Strings and Lists
string01 = 'Hello World!'
string02 = "Python Script is Awesome"
#Accessiong Values in Strings
print "string01[0]: ", string01[0]
print "string02[7:13]: ", string02[7:13]
print "string02 length = ", len(string02)
print "string02[-7:]: = ", string02[-7:]
#Replace Values
string03 = string02.replace("i","1")
print string03
list01 = ['pt1', 'pt2', 'pt3', 'pt4']
print list01[2]
print list01[1:3]
print list01[-1]
#add to list
print list01
#remove from list
del list01[2]
print list01
#list length
print len(list01)
#iterate a list
for x in list01:
    print x
#membership test
print 'pt2' in list01
print 'pt3' in list01


Please rate this tutorial below. Thanks.

Average: 5 (9 votes)

Want to Contribute?

Want to be an author? Drop us a line here we'd love to have you.

Already have a video you'd like to post? Send us a link and we'll get you going.