Data type: string

Objectives

a Every programming language is using strings as a data type. The Python way to handle strings, is shown in this station.

Instructions

Tasks:
  1. Save the following text, line by line into variables:
"Shall I compare thee to a summer's day?
Thou art more lovely and more temperate..."

Lines from Shakespeare's Sonnet 18.
  1. Return the values of all variables with the print-Function. Don’t forget the empty line.

Properties and rules

  • A string is enclosed in the following characters: single quotation, double quotation and triples of the quotation signs.
  • Strings are immutable, if you change a string, the computer has to reserve new memory and afterward release the old memory.
  • Each character is callable with an index. The index counting starts at zero.

More hints about strings kann be found at:

Wikipedia: String (computer science)

Assign strings

Each variable get’s own value.

todo: find a good equivalent to the german version...

Concatenate strings

Building longer Strings from substrings, is an often used technique.

Hint:The same result can be achieved with other data types tuple and list.

Extract parts of string

This task is possible with the slicing operator. This operation is available for all sequence data types. See also slicing.

Wildcards and strings

It is also posseble to include strings in an other string with predefined slots. The percent character(%) is used as a place-marker, followed by a single character s (%s). More about this techniques can be found in the learning station modulo.