Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Negative Indexing | Strings
Python Data Types

book
Negative Indexing

# Creating a string
string = 'codefinity'
print("Initial string: ", string)
string_1 = string[:-7]
print("Elements till 7th element from last: ", string_1)
string_2 = string[-7:-4]
print("Elements from index -7 to -5:",string_2)
string_3 = string[::-1]
print("String in reverse order: ", string_3)

12345678910
# Creating a string string = 'codefinity' print("Initial string: ", string) string_1 = string[:-7] print("Elements till 7th element from last: ", string_1) string_2 = string[-7:-4] print("Elements from index -7 to -5:",string_2) string_3 = string[::-1] print("String in reverse order: ", string_3)
copy
Opgave

Swipe to start coding

You have such a string programming. You have to carry out these steps:

  1. print elements sliced till 8th element from last(as a result you have to get: pro)
  2. print elements sliced from index -8 to -3(as a result you have to get: grammi)
  3. print elements in reverse using slice operation(as a result you have to get: gnimmargorp).

Løsning

# Creating a string
string = 'programming'
print("String: ", string)
string_1 = string[:-8]
print("Elements till 8th element from last: ", string_1)
string_2 = string[-8:-2]
print("Elements from index -8 to -3:",string_2)
string_3 = string[::-1]
print("String in reverse order: ", string_3)

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 7
single

single

# Creating a string
string = 'programming'
print("String: ", string)
string_1 = _ _ _
print("Elements till 8th element from last: ", string_1)
string_2 = _ _ _
print("Elements from index -8 to -3:",string_2)
string_3 = _ _ _
print("String in reverse order: ", string_3)

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt