Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Accessing String Elements | Data Types
Introduction to Data Analysis in Python
course content

Kursinnhold

Introduction to Data Analysis in Python

Introduction to Data Analysis in Python

1. Basics
2. Data Types
3. Control Flow
4. Functions and Modules
5. Introduction to NumPy

book
Accessing String Elements

Use square brackets with an index inside to access a particular string element. An index is not a position of a specific element since indexing in Python starts from 0. For example, watch the example below.

As you can see, the index in Python is always less than position by 1.

1234
# Initial string site = "codefinity" # Get the letters 'o' and 'y' print(site[1], site[9])
copy

To get multiple elements at once, you can use slicing. To do it, pass starting and ending indices within square brackets divided by the colon : sign. Note that the second index will not be included. For example, if you use [1:5], you will get all the elements with indices from 1 to 4. You can watch the example below.

As you can see, the last index is greater than the index of the last element by 1.

question mark

Suppose you are given the string test (in the code below). Choose the element that has index 5.

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 6

Spør AI

expand
ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

course content

Kursinnhold

Introduction to Data Analysis in Python

Introduction to Data Analysis in Python

1. Basics
2. Data Types
3. Control Flow
4. Functions and Modules
5. Introduction to NumPy

book
Accessing String Elements

Use square brackets with an index inside to access a particular string element. An index is not a position of a specific element since indexing in Python starts from 0. For example, watch the example below.

As you can see, the index in Python is always less than position by 1.

1234
# Initial string site = "codefinity" # Get the letters 'o' and 'y' print(site[1], site[9])
copy

To get multiple elements at once, you can use slicing. To do it, pass starting and ending indices within square brackets divided by the colon : sign. Note that the second index will not be included. For example, if you use [1:5], you will get all the elements with indices from 1 to 4. You can watch the example below.

As you can see, the last index is greater than the index of the last element by 1.

question mark

Suppose you are given the string test (in the code below). Choose the element that has index 5.

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 6
Vi beklager at noe gikk galt. Hva skjedde?
some-alt