Made byBobr AI

Python String Length and Indexing Guide | len() & Indexes

Master Python strings with this guide on the len() function, zero-based indexing, negative indexes, and avoiding common IndexError exceptions.

#python#string-methods#coding-basics#programming-tutorial#len-function#indexerror

הפונקציה len() בפייתון

פונקציה מובנית המחזירה את אורך המחרוזת (מספר התווים).

תחביר: len(string)
  • סופרת את כל סוגי התווים: אותיות, מספרים וסימנים.
  • שימוש נפוץ: לולאות, בדיקות תקינות קלט וחיתוך מחרוזות.
⚠️

שים לב!

רווח הוא תו לכל דבר ונחשב בספירה!

word = "Hello"\nprint(len(word)) # 5
example.py
Made byBobr AI

נקודות חשובות על len()

  • len() סופרת כמה תווים יש.
  • היא לא נותנת מיקום (אינדקס).
  • האינדקסים מתחילים מ־0.

נוסחה חשובה:

index_last = len(word) - 1

word = "Python"
print(len(word)) # 6
Made byBobr AI

Indexes in Python

An index is the position of a character in a string. It allows you to access specific parts of text data.

⚠️ Warning: Indexes start at 0
Letter:
P
y
t
h
o
n
Index:
0
1
2
3
4
5
script.py
word = "Python"
print(word[0]) # P
print(word[2]) # t
</> Python Programming
Made byBobr AI

Negative Indexes

Python allows negative indexes. They count from the end of the string.

0
P
-6
1
y
-5
2
t
-4
3
h
-3
4
o
-2
5
n
-1
Positive Index
String Character
Negative Index
print(word[-1]) # n
print(word[-2]) # o
Made byBobr AI

Using len() With Indexes

The connection between length and the last index

word = "Python"
len(word) # Returns 6
! Key Concept: Indexes start at 0.

The last index is always len(word) - 1.

print(word[len(word) - 1]) # n
0
P
1
y
2
t
3
h
4
o
5
n
Length = 6
Calculation
6 - 1 = 5
len(word) - 1
Made byBobr AI

Common Error: IndexError

What happens when you access an index that doesn't exist?

H
[0]
i
[1]
?
[2]

Strings are zero-indexed. 'H' is at index 0 and 'i' is at index 1. Trying to access index 2 causes an error because it's out of bounds.

script.py
word = "Hi"
print(word[2]) # Error happens here!
IndexError
string index out of range
Made byBobr AI

מהו אינדקס?

אינדקס הוא המיקום של תו במחרוזת.

P
0
y
1
t
2
h
3
o
4
n
5
!

שימו לב: האינדקס הראשון הוא 0

Made byBobr AI

אינדקסים שליליים

אינדקס שלילי מתחיל מהסוף ומאפשר גישה נוחה לאיברים האחרונים.

הסבר:
1- הוא התו האחרון, 2- הוא התו הלפני אחרון, וכן הלאה. השיטה חוסכת חישוב אורך הרשימה ידנית.

word = "Python"
print(word[-1]) 
# Output: n
word = "Python"
P
y
t
h
o
n
-6
-5
-4
-3
-2
-1
Made byBobr AI

הקשר בין len() לאינדקסים

word = "Python"
len(word) # 6
P
0
y
1
t
2
h
3
o
4
n
5

האינדקס האחרון הוא: len(word) - 1

Made byBobr AI

שגיאה נפוצה

word = "Hi"
print(word[2]) # Error!

שגיאת אינדקס

נקבל שגיאה כי אין אינדקס 2 ברשימה או במחרוזת.

אינדקסים קיימים: 0 (H), 1 (i)
H
0
i
1
?
2
Made byBobr AI
Bobr AI

DESIGNER-MADE
PRESENTATION,
GENERATED FROM
YOUR PROMPT

Create your own professional slide deck with real images, data charts, and unique design in under a minute.

Generate For Free

Python String Length and Indexing Guide | len() & Indexes

Master Python strings with this guide on the len() function, zero-based indexing, negative indexes, and avoiding common IndexError exceptions.

הפונקציה len() בפייתון

פונקציה מובנית המחזירה את אורך המחרוזת (מספר התווים).

len(string)

סופרת את כל סוגי התווים: אותיות, מספרים וסימנים.

שימוש נפוץ: לולאות, בדיקות תקינות קלט וחיתוך מחרוזות.

רווח הוא תו לכל דבר ונחשב בספירה!

word = <span style="color: #CE9178;">"Hello"</span>\n<span style="color: #569CD6;">print</span>(<span style="color: #DCDCAA;">len</span>(word)) <span style="color: #6A9955;"># 5</span>

נקודות חשובות על len()

len() סופרת כמה תווים יש.

היא לא נותנת מיקום (אינדקס).

האינדקסים מתחילים מ־0.

word = <span style="color: #CE9178;">"Python"</span> <span style="color: #569CD6;">print</span>(<span style="color: #DCDCAA;">len</span>(word)) <span style="color: #6A9955;"># 6</span>

נוסחה חשובה:

index_last = len(word) - 1

Indexes in Python

An index is the position of a character in a string. It allows you to access specific parts of text data.

Warning: Indexes start at 0

# P

# t

Negative Indexes

Python allows negative indexes. They count from the end of the string.

n

o

Using len() With Indexes

The connection between length and the last index

word = "Python"

len(word) # Returns 6

The last index is always <strong>len(word) - 1</strong>.

Common Error: IndexError

What happens when you access an index that doesn't exist?

Strings are zero-indexed. <strong style="color: #4CAF50;">'H'</strong> is at index 0 and <strong style="color: #4CAF50;">'i'</strong> is at index 1. Trying to access index <strong style="color: #FF5252;">2</strong> causes an error because it's out of bounds.

string index out of range

מהו אינדקס?

אינדקס הוא המיקום של תו במחרוזת.

שימו לב: האינדקס הראשון הוא 0

P

y

t

h

o

n

0

1

2

3

4

5

אינדקסים שליליים

אינדקס שלילי מתחיל מהסוף ומאפשר גישה נוחה לאיברים האחרונים.

1- הוא התו האחרון, 2- הוא התו הלפני אחרון, וכן הלאה. השיטה חוסכת חישוב אורך הרשימה ידנית.

word = "Python" print(word[-1]) # Output: n

הקשר בין len() לאינדקסים

האינדקס האחרון הוא:

שגיאה נפוצה

נקבל שגיאה כי אין אינדקס 2 ברשימה או במחרוזת.

  • python
  • string-methods
  • coding-basics
  • programming-tutorial
  • len-function
  • indexerror