Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Python Programming Fundamentals
< Prev   CONTENTS   Next >

1.13 Strings

Strings are another type of data in Python. A string is a sequence of characters.

name = ' Sophus Lie '

print (" A famous Norwegian Mathematician is " , name )

This is a short program that initializes a variable called name to the string 'Sophus Lie'. A string literal is an actual string value written in your program. String literals are delimited by either double or single quotes. Delimited means that they start and end with quotes. In the code above the string literal Sophus Lie is delimited by single quotes. The string A famous Norwegian Mathematician is is delimited by double quotes. If you use a single quote at the beginning of a string literal, you must use a single quote at the end of the string literal. Delimiters must come in matching pairs.

Strings are one type of sequence in Python. There are other kinds of sequences in Python as well, such as lists which we'll look at in a couple of chapters. Python supports operations on sequences. For instance, you can get an individual item from a sequence. Writing,

print ( name [0])

will print the first character of the string that name references. The 0 is called an index. Each subsequent character is assigned a subsequent position in the string. Notice the first position in the string is assigned 0 as its index. The second character is assigned index 1, and so on. Strings and their operations are discussed in more detail in Chap. 3.

Practice 1.11 Write the three line program given in the two listings on p. 24.Then, without writing the string literal “house”, modify it to print the string“house” to the screen using string indexing. HINT:You can add strings togetherto build a new string. So,name = "Sophus" + "Lie"will result in name referring to the string “Sophus Lie”.

1.14 Integer to String Conversion and Back Again

It is possible in Python to convert an integer to a string. For instance,

x = s t r (83)

print ( x [0])

print ( x [1]) y = i n t (x) print (y)

This program converts 83 to '83' and back again. Integers and floats can be converted to a string by using the str conversion operator. Likewise, an integer or a float contained in a string can be converted to its numeric equivalent by using the int or float conversion operator. Conversion between numeric types and string types is frequently used in programs especially when producing output and getting input.

Conversion of numeric values to strings should not be confused with ASCII conversion. Integers may represent ASCII codes for characters. If you want to convert an integer to its ASCII character equivalent you use the chr conversion operator. For instance, chr(83) is 'S'. Likewise, if you want to convert a character to its ASCII code equivalent you use the or d conversion operator. So ord('S') is equal to 83.

Operation

Operator

Comments

Indexing

s[x]

Yields the xth character of the string s. The index is zero based, so s[0] is the first character.

Concatenation

s + t

Yields the juxtaposition of the strings s and t.

Length

len(s)

Yields the number of characters in s.

Ordinal

Value

ord(c)

Yields the ordinal value of a character c.

The ordinal value is the ASCII code of the character.

Character

Value

chr(x)

Yields the character that corresponds to the

ASCII value of x.

String

Conversion

str(x)

Yields the string representation of the value of x.

The value of x may be an int, float, or other type of value.

Integer

Conversion

int(s)

Yields the integer value contained in the string s. If s

does not contain an integer an error will occur.

Float

Conversion

float(s)

Yields the float value contained in the string s. If s

does not contain a float an error will occur.

Fig. 1.19 String operations

Practice 1.12 Change the program above to convert 83 to its ASCII characterequivalent. Save the value in a variable and print the following to the screen inthe exact format you see here.The ASCII character equivalent of 83 i s S.

You might have noticed in Fig. 1.19 there is an operator called int and another called float. Both of these operators are also numeric operators and appear in Fig. 1.18. This is called an overloaded operator because int and float are operators that work for both numeric and string operands. Python supports overloaded operators like this. This is a nice feature of the language since both versions of int and float do similar things.

 
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Philosophy
Political science
Psychology
Religion
Sociology
Travel