##################### # Python Class Lecture Notes & Tests # 2014-02-12 Wed # Numbers and Strings # # # # # ##################### #!/user/bin/python intvar1 = 5 intvar2 = 4 intvar3 = intvar1 / intvar2 print intvar3 print intvar1 / float(intvar2) ### Strings # if you have a string that contains one type of quotes, you can use the other type to enclose them: print "I wanted to tell him 'use the one you want'." #Otherwise, you can escape any quotes if within same-type quotes: print "I wanted to tell him \"use the one you 'want'\"." varhello = ''' hello there i am a string with some new lines ''' print varhello # When combining object values with strings (i.e., printing them together), it's useful to use string formatting: name = 'Jose' value = 54 print '%s is %s years old' % (name, value) # %s is a token in the string to print, and the (name, value) is a tuple # The 'template' is the string, which contains %s tokens. This string is followed by a % sign and a tuple of values to be inserted. The number of tuple elements and tokens must match. alpha = 'abcdefghijklmnop' print alpha[4:6] print alpha[7:15] min = 0 max = 4 print alpha[min:max] # Strings are sequences of characters, and as such they can be sliced by index: mystr = '2359:4323:4444:1112' first_value = mystr[0:4] # slice and return first 4 letters print first_value # '2359' # str.split() and str.join(): string->list, list->string # Here is our first method that returns a list: mystr = '234:432234:23:9:14' elements = mystr.split(':') print elements # ['234', '432234', '23', '9', '14'] # split is called on a string object, takes a substring as an argument, and splits the string on the substring, dividing it into individual string objects. # The substring is removed and does not appear in the string elements. # join is called on the joining character: element_list = ['234', '432234', '23', '9', '14'] mystr = ':'.join(element_list) print mystr # 234:432234:23:9:14 mystr = '\n'.join(element_list) print mystr # prints the numbers on a separate line # print is actually a convenience function - it outputs strings to the screen, but it also adds a newline to the end: print 'hey' # prints 'hey\n' print 'there' # prints 'there\n' # if we want to omit the newline, we can add a comma to the end - now a space is added: print 'hey', print 'there' # prints 'hey there' # if we want to avoid anything being added to our strings we can use sys.stdout.write() -- more on this later: import sys sys.stdout.write('hey') sys.stdout.write('there') # prints 'heythere' print print 'joe is',5,'years old' print '*****' mylist = ['a', 'b', 'c'] for i in mylist: print i print '*****' mylist = ['a', 'b', 'c'] for i in mylist: print 'xxxx' print i print '*****' filename = 'test.txt' for line in open(filename).readlines(): print line
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question