Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Indexering och Skivning | Strängar
Datatyper i Python

bookIndexering och Skivning

123
s = "python" print(s[0]) # 'p' (first character) print(s[5]) # 'n' (sixth character)
copy
123
s = "python" print(s[-1]) # 'n' (last character) print(s[-2]) # 'o' (second from the end)
copy
12
s = "python" print(s[10]) # IndexError: string index out of range
copy
12
s = "python" s[0] = 'P' # TypeError: 'str' object does not support item assignment
copy
12345
s = "python" print(s[1:4]) # 'yth' (indices 1,2,3) print(s[:4]) # 'pyth' (start defaults to 0) print(s[3:]) # 'hon' (stop defaults to len(s)) print(s[::2]) # 'pto' (every 2nd character)
copy
12
s = "python" print(s[0:100]) # 'python'
copy
123
s = "python" print(s[-3:]) # 'hon' (last three) print(s[::-1]) # 'nohtyp' (reverse)
copy
Note
Note
question mark

Select the correct answer

question mark

Select the correct answer

question mark

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 2

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

bookIndexering och Skivning

Svep för att visa menyn

123
s = "python" print(s[0]) # 'p' (first character) print(s[5]) # 'n' (sixth character)
copy
123
s = "python" print(s[-1]) # 'n' (last character) print(s[-2]) # 'o' (second from the end)
copy
12
s = "python" print(s[10]) # IndexError: string index out of range
copy
12
s = "python" s[0] = 'P' # TypeError: 'str' object does not support item assignment
copy
12345
s = "python" print(s[1:4]) # 'yth' (indices 1,2,3) print(s[:4]) # 'pyth' (start defaults to 0) print(s[3:]) # 'hon' (stop defaults to len(s)) print(s[::2]) # 'pto' (every 2nd character)
copy
12
s = "python" print(s[0:100]) # 'python'
copy
123
s = "python" print(s[-3:]) # 'hon' (last three) print(s[::-1]) # 'nohtyp' (reverse)
copy
Note
Note
question mark

Select the correct answer

question mark

Select the correct answer

question mark

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 2
some-alt