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?
< previousTable of Contentsnext >

6.2 STRING OPERATIONS

String operations allow us to manipulate, evaluate, and transform strings.

CONCATENATION

Concatenation allows us to join two or more strings together by using the + operator.


greeting = "Hello" + "  " + "World!"
print(greeting)
# This will print out 'Hello World!' to the console

REPETITION

We can repeat a string of a specified number of times using the * operator.


greeting = "Hello! " * 2
print(greeting)
#Ouputps Hello! Hello! 

INDEXING

We can access individual characters in a string by their position. Strings in Python are zero-indexed, meaning that the first character of a string is at index zero, the second one is at index one, and so forth. NB! The indexes have to be integers.


greeting = "Hello World!"
print(greeting[0])
#Outputs the character ‘H’ from the string greeting. 

Python also supports negative indexing where -1 refers to the last element, -2 to the second to last, etc.


greeting = "Hello World!"
first_letter = greeting[0]  # 'H'
last_letter = greeting[-1]  # '!'

SLICING

Slicing enables us to extract a part of a string, a substring, by specifying a range we want to extract.


slice = string[start:stop:step]

  • start is the index where the slice starts. It’s inclusive of the specified index. If start is omitted, then slicing starts from the beginning by default.
  • stop is the index where the slice ends. Note that it is exclusive, meaning that the stop index you’ve specified will not be included in the slice. However, if stop is omitted then slicing goes up to and including the final character of the string.
  • Lastly, step is the step count between elements in the slice. For example, if the step is 2, we skip every other character. The step can also take positive and negative values. By default, the step is 1.
  • Note that all the items in the brackets are optional.

In this example, we want to extract the word "are" from the full string, we can do so by accessing the elements through slicing according to their index test_string[4:8]. The step can also be negative, which allows us to do cool things like reversing a string test_string[::-1].

THE IN OPERATOR

in is a boolean operator that allows you to determine whether a value exists within a string. It returns True if the specified value is found and False if it’s not.

Here, we try to see if the word "you" exists within the string. We can use the in-operator to do so. We can see that the program outputs True meaning that the word "you" is present in the string.

Note that the in operator is case sensitive, meaning that the case of the characters must match exactly the substring you’re trying to check for. As you can see, as we changed the word to start with a capital letter "You", now that we check to see if the word is in the string, the result is False due to case sensitivity.

The in operator can also be combined with not for negation. not in allows you to check if a substring does not exist within another string. As you can see the result of our previous example is now True.

KEY THINGS TO REMEMBER

  • Concatenation joins two or more strings into one using the + operator.
  • Repetition repeats a string desired number of times using the * operator.
  • Characters are zero-indexed, meaning the first character is at index 0.
  • Slicing allows us to access a substring of a string.
  • The in operator checks if a substring exists within another string and returns either True or False. not in can be used for negation.

SELF-CONTROL EXERCISES

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