Institute of Computer Science
  1. Courses
  2. 2024/25 fall
  3. Introduction to Programming (MTAT.03.236)
ET
Log in

Introduction to Programming 2024/25 fall

  • Home Page
  • Introduction
  • Expressions
  • Conditional Execution
  • Functions
  • Iterations
  • Strings
  • Files
  • Lists
  • Graphics?
< eelmineTable of Contentsnext >

6.1 WHAT IS A STRING?

You should be quite familiar with the concept by now, as you’ve come across it in numerous examples and exercises. However, to reiterate, a string is a sequence of characters used to represent text. The characters can include alphabets, digits, symbols, and spaces.

CREATING STRINGS

Strings in Python can be created by enclosing characters inside single quotes '...', double quotes "...", or even triple quotes ... or """...""" for multi-line strings.

An important thing to remember is that strings are immutable, meaning that once you’ve defined a String, the characters within it cannot be changed. Attempting to modify characters in a string directly will result in a TypeError.

Here, we're trying to change the second character of the string "Welcome!" to 't' and given the immutability property of Strings, this results in a TypeError.

If we want to change the string attached to this variable, we must assign it a new string as shown below.

CHARACTERISTICS OF STRINGS

There are a few concepts that you should keep in mind when working with strings. We will dive deeper into those later in this paragraph, but for now, here’s a quick overview.

Strings in Python are immutable, meaning we cannot alter them once a string is created. If we want to assign a new string to the same variable, we have to replace the existing string by assigning a new one.

Each character in a string can be accessed using its index, with indexing starting at 0 for the first character. Strings can also be sliced, allowing for the extraction of substrings.

Strings are iterable, meaning you can loop through each character in a string using a for loop.

KEY THINGS TO REMEMBER

  • String elements cannot be altered after they are created.
  • Each character in a string can be accessed using its index. Indexing starts at 0 for the first character.
  • Strings are iterable, allowing them to be looped over with a for loop
< eelmineTable of Contentsnext >

  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment