10_2_string_notes

# 10_2_string_notes - Strings A string is a sequence just...

This preview shows pages 1–3. Sign up to view the full content.

Strings ------- A string is a sequence, just like a list. It contains lots of elements in sequence. In the case of strings, the elements are characters. >>> s = "Take me to your leader, foolish Earthling" We can find what type a variable is by using the Python function 'type'. >>> type(s) <type 'str'> Strings can contain any characters - upper and lower case letters, punctuation symbols, and numerals. Don't forget that numbers and numerals aren't the same type: >>> courseNumber = 1315 >>> courseName = "1315" >>> type(courseNumber) <type 'int'> >>> type(courseName) <type 'str'> But we can convert numbers into strings using the function 'str' >>> str(courseNumber) '1315' >>> type(str(courseNumber)) <type 'str'> and if a string is a string of digits, we can go the opposite way using the function 'int' >>> int(courseName) 1315 >>> type(int(courseName)) <type 'int'> String length ------------- Like lists, strings have a length that you can find using 'len'. >>> len(courseName) 4 Indices ------- We often want to refer to items in a string, and we do so by using indices. In mathematics, we use subscripts to indicate an index. In Python, we use square brackets. So if s is a sequence (list or string) and we want to find item number i in the sequence, where i is an integer variable, we say s[i]. Remember that we always start counting at 0. >>> s 'Take me to your leader, foolish Earthling' >>> s[0]

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
'T' >>> s[1] 'a' >>> s[2] 'k' We can of course put index variables in the index position: >>> index = 4 >>> s[index] ' ' (That's the space between 'Take' and 'me'.) And we can put expressions there too: >>> s[len(s)-1] 'g' because len(s)==41, len(s)-1 == 40. s[40] == 'g'. In general, the last character in any string s is s[len(s)-1]. If we try to address a character that doesn't exist, typically by making an "off by one" error and running one character beyond the end of the string, we get the following very common run-time error. >>> s[len(s)] The error was: 41 Sequence index out of range. The index you're using goes beyond the size of that data (too low or high). For instance, maybe you tried to access OurArray[10] and OurArray only has 5 elements in it. in file /Applications/jes-4-3.app/Contents/Resources/Java/JESThread.py, on line 62, in function run in file <input>, on line 1, in function ? IndexError: index out of range: 41 Characters ---------- In some programming languages, strings contain characters, and characters are a different type of data than the strings they are contained in. In Python, a 'character' is nothing other than a string of length one. This makes things much simpler: >>> s[0] 'T' >>> type(s[0]) <type 'str'> Null strings ------------ You can have an empty or null string. It seems a bit weird, but there you go. A null string has no character in it, and so is written by consecutive
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 7

10_2_string_notes - Strings A string is a sequence just...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online