Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Indeksointi ja Viipalointi | Merkkijonot
Tietotyypit Pythonissa

bookIndeksointi ja Viipalointi

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 2

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

bookIndeksointi ja Viipalointi

Pyyhkäise näyttääksesi valikon

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 2
some-alt