Arvutiteaduse instituut
  1. Kursused
  2. 2024/25 sügis
  3. Programmeerimise alused (MTAT.03.236)
EN
Logi sisse

Programmeerimise alused 2024/25 sügis

  • 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 >

  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Courses’i keskkonna kasutustingimused