Matthew Parsons M.Eng Department of Mathematical and Physical Sciences Concordia University of Edmonton Introduction to Computing Science Numbers and Strings Programmers While Coding Objectives This lecture covers Numbers (revisited) Integer Floating point Complex Strings Defining Escape sequences Multi-line strings String operations CMPUT 111 2 Numbers Numbers in Python are of three types – integers (int), floating point (float) and complex (complex) numbers. Examples: integers: 5; -4; 0. floating point numbers (or floats for short): 3.14; 3.1E-4; 7E10. complex numbers: (-5+4j); (2.3 - 4.6j); j. There is no separate 'long int' type. The default integer type can be any large value. CMPUT 111 3 Numbers and Strings Numbers are fundamental to computing—in fact, crunching numbers is what computers were invented to do—but there are many other kinds of data in the world as well, such as addresses, pictures, and music. There is also a non-numeric data type that represents text, such as the words in this sentence or a strand of DNA. Computers may have been invented to do arithmetic, but these days, most of them spend a lot of their time processing text. Computers create text, store it, search it, and move it from one place to another. CMPUT 111 4 Strings String is a sequence of characters (letters, numbers, and symbols). Strings are composed by words. The words can be in English or any other language that is supported in the Unicode standard, which means almost any language in the world. Strings are used in almost every Python program. By default, all Python strings are in Unicode. There are no "ASCII-only" strings because Unicode is a superset of ASCII. Python strings are immutable, i.e. they cannot be changed. The len(str) function returns the length of a string. CMPUT 111 5 Strings In Python, we indicate that a value is a string by putting either single or double quotes around it: >>> 'This is a string!' 'This is a string!' >>> "This is also a string!" 'This is also a string!' You can specify multi-line strings using triple quotes - (""" or '''). You can use single quotes and double quotes freely within the triple quotes. An example is: '''This is a multi-line string. This is the second line. "We can quote." ''' CMPUT 111 6 Logical And Physical Lines A physical line is what you see when you write the program. A logical line is what Python sees as a single statement. Python implicitly assumes that each physical line corresponds to a logical line. An example of a logical line is a statement like print('Hello World') – if this was on a line by itself (as you see it in an editor), then this also corresponds to a physical line. Implicitly, Python encourages the use of a single statement per line which makes code more readable. Writing a logical line spanning many physical lines is referred to as explicit line joining. CMPUT 111 7 Strings Suppose, you want to have a string which contains a single quote ('), how will you specify this string? For example, the string is What's your name?. You cannot specify 'What's your name?' because Python will be confused as to where the string starts and ends. You will have to specify that this single quote does not indicate the end of the string. This can be done with the help of what is called an escape sequence. You specify the single quote as \' - notice the backslash. Now, you can specify the string as 'What\'s your name?'. Another way of specifying this specific string would be "What's your name?" i.e. using double quotes. CMPUT 111 8 Escape Characters/Sequences Escape Sequence Description \n ASCII end of line \\ Backslash \' Single quote \" \a \t \v Double quote ASCII Bell ASCII Horizontal Tab ASCII Vertical Tab CMPUT 111 9 Strings A two-line string can be coded as a triple-quoted string. an escape sequence for the newline character - \n to indicate the start of a new line. A single backslash at the end of the line indicates that the string is continued in the next line, but no newline is added. "This is the first sentence.\ This is the second sentence." CMPUT 111 10 The input() function The built-in function input reads a single line of text from the keyboard. It returns whatever the user enters as a string, even if it looks like a number: >>> line = input() CS rocks! >>> print(line) CS rocks! >>> line = input() 123 >>> print(line*2) CMPUT 111 11 String Operations We can join two strings together by putting them side by side: >>> 'This is a' 'string!' 'This is astring!' It’s almost always clearer to join strings with +. When + has two string operands, then it is referred to as the concatenation operator: >>> 'This is a' + ' string!' 'This is a string!' Since the + operator is used for both numeric addition and for string concatenation, we call this an overloaded operator. It performs different functions based on the type of operands that it is applied to. CMPUT 111 12 String Operations Python can repeat a string using the * operator. If the integer is less than or equals to zero, then this operator yields the empty string (a string containing no characters). For each of the following expressions, what value will the expression give? Verify your answers by typing the expressions into the Python shell. a) 'Comp' 'Sci' b) 'Computing' + ' Science' c) '1' * 3 d) '111' * 0 CMPUT 111 13 String Operations Strings can be subscripted (indexed). Substrings can be specified with the slice notation: two indices separated by a colon. What is the output of the following code? >>> >>> >>> >>> >>> >>> >>> >>> sentence = 'Programming' + ' is fun!' print(sentence) print(sentence[0]) print(sentence[5]) print(sentence[0:10]) print(sentence[5:10]) print(sentence[:5]) print(sentence[5:]) CMPUT 111 14 Question 1 For each of the following phrases, express them as Python strings using the appropriate type of quotation marks (single, double or triple) and, if necessary, escape sequences: a) They’ll hibernate during the winter. b) "Absolutely not," he said. c) "He said, ' Absolutely not, "' recalled Mel. d) left\right CMPUT 111 15 Question 2 Rewrite the following string using single or double quotes instead of triple quotes: '' 'A B C'' ' Use the built-in function len to find the length of the empty string. CMPUT 111 16