 |  |

 |
 |  |  |
 | Welcome to CS1315. Click on the python to add comments.
|  |
 |  |  |
|
This page removed for FERPA compliance
|
        |
String Basics
def stringBasics():
#Single, Double and Tripple Quotes
print 'That is all Folks'
#print 'That's all Folks'
print """That's all
Folks"""
#Five letters in an array
for i in "Folks":
print i
#Actually five letter codes in an array (ASCREMOVED --> Unicode)
for i in "Folks":
print ord(i)
#Unicode example
print u"\u03A9 \u03A8 \u03A6"
print u"\u0394 \u03A3 \u0398"
#Escape characters 'backslash'
print 'That \t is \t all \t Folks' #\t = tab
print 'That \b is \b all \b Folks' #\b = backspace
print 'That \n is \n all \n Folks' #\n = enter/return/newline
#r Means raw (ignore escape characters)
print r"C:\news folder\text\bold headlines"
#Use + to Concatenate
print 'That' + ' ' + 'is' + ' ' + 'all' + ' ' + 'Folks'
w1 = 'That'
space = ' '
w2 = 'is'
w3 = 'all'
w4 = 'Folks'
s1 = w1 + w2 + w3 + w4
print w1 + space + w2 + space + w3 + space + w4
print s1
#len() is length of the string
print len('That')
print len(s1)
#Accessing parts of the array (START COUNTING AT ZERO)
print w1[0] #Start counting at zero
print w1[3]
print w1[1:3] #Don't include last number
print w1[:3] #If omit first number, it is assumed to be zero. Again, last not included
print w1[1:] #If omit second number, you get all the way to the end
print w1[:] #Omitting both numbers gets you all characters
print w1[-3:] #Removes length minus 3 from the beginning
print w1[:-3] #Removes 3 from the end
Link to this Page