Indexering en Slicing
123s = "python" print(s[0]) # 'p' (first character) print(s[5]) # 'n' (sixth character)
123s = "python" print(s[-1]) # 'n' (last character) print(s[-2]) # 'o' (second from the end)
12s = "python" print(s[10]) # IndexError: string index out of range
12s = "python" s[0] = 'P' # TypeError: 'str' object does not support item assignment
12345s = "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)
12s = "python" print(s[0:100]) # 'python'
123s = "python" print(s[-3:]) # 'hon' (last three) print(s[::-1]) # 'nohtyp' (reverse)
Note
Was alles duidelijk?
Bedankt voor je feedback!
Sectie 3. Hoofdstuk 2
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Geweldig!
Completion tarief verbeterd naar 3.45
Indexering en Slicing
Veeg om het menu te tonen
123s = "python" print(s[0]) # 'p' (first character) print(s[5]) # 'n' (sixth character)
123s = "python" print(s[-1]) # 'n' (last character) print(s[-2]) # 'o' (second from the end)
12s = "python" print(s[10]) # IndexError: string index out of range
12s = "python" s[0] = 'P' # TypeError: 'str' object does not support item assignment
12345s = "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)
12s = "python" print(s[0:100]) # 'python'
123s = "python" print(s[-3:]) # 'hon' (last three) print(s[::-1]) # 'nohtyp' (reverse)
Note
Was alles duidelijk?
Bedankt voor je feedback!
Sectie 3. Hoofdstuk 2