PythonPython · Chapterبەش 6 · Lectureوانە 6.1

Working with Stringsکارکردن لەگەڵ زنجیرەنووسەکان

Text in Python: measure strings with len(), read characters by index, take slices, join and search them, use string methods, and compare strings — including why "A" < "a" is True and how Kurdish letters compare.

دەق لە : پێوانی بە len()، خوێندنەوەی بە ، ، و گەڕان تێیاندا، و بەراوردکردنی — لەگەڵ ئەوەی بۆچی "⁦A" < "a⁩" ڕاستە و پیتە کوردییەکان چۆن بەراورد دەکرێن.

19 min read20 خولەک خوێندنەوە · 20 stepsهەنگاو

What you will learnچی فێر دەبیت

  • Read single characters and slices of a string with positive and negative indexes.
  • Explain why strings are immutable and build a new string instead of changing one.
  • Join, repeat and search strings, and use the common string methods.
  • Use the case, test (is…()) and search methods, and choose between find() and index().
  • Predict the result of comparing two strings using code points, and compare without caring about case.
  • و بەشەکانی بە موجەب و بخوێنیتەوە.
  • ڕوونی بکەیتەوە بۆچی و لەبری گۆڕین نوێ دروست بکەیت.
  • بلکێنیت، دووبارەیان بکەیتەوە و تێیاندا بگەڕێیت و باوترین بەکار بهێنیت.
  • گەورە و بچووکی پیت، پشکنین (is…()) و گەڕان بەکار بهێنیت و لە نێوان find() و index()دا هەڵبژێریت.
  • ئەنجامی بەراوردکردنی دوو بە یارمەتیی پێشبینی بکەیت و بەبێ گرنگیدان بە گەورە و بچووکی پیت بەراورد بکەیت.

Text in Python: stringsدەق لە Pythonـدا: زنجیرەنووسەکان

In Python, every piece of text is a string (type str): a row of characters between quotes. A character can be a letter, a digit, a space, a sign such as !, or a Kurdish letter. Single quotes 'Hawler' and double quotes "Hawler" make exactly the same string, so pick one style and keep it.

لە هەر پارچە دەقێک (جۆری str): ڕیزێک لە نێوان دوو . دەکرێت پیتێک، ژمارەیەک، ، هێمایەکی وەک ! یان پیتێکی کوردی بێت. 'Hawler' و "Hawler" ڕێک هەمان ؛ بۆیە یەکێک لە دوو شێوازەکە هەڵبژێرە و لەسەری بەردەوام بە.

The built-in function len() counts the characters of a string. Spaces and signs count too, and the empty string "" has length 0.

len() دەژمێرێت. و هێماکانیش دەژمێردرێن؛ بەتاڵ "" درێژییەکەی 0ە.

Measuring strings with len()پێوانی زنجیرەنووسەکان بە len()length.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
city = "Hawler"
print(city)
print(len(city))
print(len("Hello, Kurdistan!"))
print(len(""))
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

A variable that holds a string.

کە هەڵدەگرێت.

Lineدێڕی 3

len() returns the number of characters: 6.

len() ژمارەی دەگەڕێنێتەوە: 6.

Lineدێڕی 5

The empty string has no characters at all.

بەتاڵ هیچ تێدا نییە.

Beads on a tasbihدەنکی تەزبیح

A string is like a tasbih (prayer beads): beads in a fixed order on one thread. Each bead is one character, and you can count them.

وەک تەزبیحێک وایە: دەنکەکان بە ڕیزێکی دیاریکراو بە یەک داوەوەن. هەر دەنکێک و دەتوانیت بیانژمێریت.

Where the analogy stops workingسنووری ئەم نموونەیە

On a real tasbih you could replace one bead. A Python string can never be changed: you build a new string instead (see Strings cannot be changed).

لە تەزبیحی ڕاستەقینەدا دەتوانیت دەنکێک بگۆڕیت. بەڵام هەرگیز ناگۆڕدرێت: لەبری ئەوە نوێ دروست دەکەیت (بڕوانە زنجیرەنووسەکان ناگۆڕدرێن).

Quick checkپشکنینی خێراWhat does len("Hello, Kurdistan!") give?len("Hello, Kurdistan!") چی دەدات؟

05- نووسین له پایثۆندا Working With Strings In PythonMR. Ant · Kurdish (Sorani)کوردی (سۆرانی) · 26K viewsبینین · 846 likesلایک

In Kurdish: working with strings in Python, including indexes and string functions.

بە کوردی: کارکردن لەگەڵ لە ، لەوانە و .

Kurdish (Sorani), whole video (14:01): part 5 of MR. Ant's Python course.

ڤیدیۆیەکی کوردی (سۆرانی)، هەموو ڤیدیۆکە ⁦(14:01)⁩: بەشی 5 لە کۆرسی ⁦MR. Ant⁩.

Every character has an indexهەر نووسەیەک ئیندێکسێکی هەیە

Each character has a position number called its index. Python counts from 0, not from 1: the first character is word[0], the second is word[1]. So the last index is always len(word) - 1. The picture shows the indexes of "PYTHON".

هەر ژمارەی شوێنێکی هەیە کە پێی دەوترێت . لە 0ەوە دەژمێرێت، نەک لە 1: یەکەم word[0]ە و دووەمیان word[1]. بۆیە دوایین هەمیشە len(word) - 1ە. وێنەکە "PYTHON" پیشان دەدات.

The word PYTHON in six boxes. Above the boxes are the indexes 0 to 5; below them the negative indexes -6 to -1. The boxes Y, T and H (indexes 1 to 3) are highlighted and a bracket marks them as word[1:4], which gives "YTH"; the box O at index 4 is marked as the stop index, which is not included.وشەی PYTHON لە شەش چوارگۆشەدا. لە سەرەوەی چوارگۆشەکان ئیندێکسەکانی 0 تا 5 و لە خوارەوەیان ئیندێکسە سالبەکانی -6 تا -1 نووسراون. چوارگۆشەکانی Y، T و H (ئیندێکسەکانی 1 تا 3) ڕەنگ کراون و کەوانەیەک وەک word[1:4] نیشانیان دەدات کە "YTH" دەدات؛ چوارگۆشەی O لە ئیندێکسی 4 وەک کۆتایی نیشان کراوە کە خۆی تێدا نییە.
Indexes count from 0 at the left and from −1-1 at the right. لە چەپەوە لە 0 و لە ڕاستەوە لە −1-1ەوە دەژمێردرێن.CêIT
Reading characters by indexخوێندنەوەی نووسەکان بە ئیندێکسindex.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
word = "PYTHON"
print(word[0])
print(word[1])
print(word[5])
print(word[len(word) - 1])
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4-5

Index 5 is the last one, because the length is 6. len(word) - 1 works for a string of any length.

5 دوایینە، چونکە درێژییەکە 6ە. len(word) - 1 بۆ هەر درێژییەک کار دەکات.

There is no character at index 6. Asking for it stops the program with an IndexError:

لە 6دا هیچ نییە. داواکردنی ئەو بە IndexError دەوەستێنێت:

index_error.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
word = "PYTHON"
print(word[6])
Quick checkپشکنینی خێراword = "Erbil". What is word[1]?word = "Erbil". word[1] چییە؟

Every character has an indexهەر نووسەیەک ئیندێکسێکی هەیە

Counting from the endژماردن لە کۆتاییەوە

Python can also count from the end with negative indexes: word[-1] is the last character, word[-2] the one before it, and word[-len(word)] is the first. Inside, Python just adds the length: word[-1] means word[len(word) - 1]. It is the easiest way to reach the last character without len().

دەتوانێت لە کۆتاییشەوە بژمێرێت، بە : word[-1] دوایین ، word[-2] ئەوەی پێش ئەوە و word[-len(word)] یەکەمیانە. لە ناوەوە تەنها درێژییەکە زیاد دەکات: word[-1] واتە word[len(word) - 1]. ئەمە ئاسانترین ڕێگەیە بۆ گەیشتن بە دوایین بەبێ len().

negative.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
word = "PYTHON"
print(word[-1])
print(word[-2])
print(word[-6])
Quick checkپشکنینی خێراword = "Kurdistan". What is word[-3]?word = "Kurdistan". word[-3] چییە؟

Slicing: taking part of a stringلەتکردن: وەرگرتنی بەشێک لە زنجیرەنووسەیەک

A slice word[start:stop] gives the characters from index start up to, but not including, index stop. So word[1:4] takes indexes 1, 2 and 3 (see the picture above). Leave out start to begin at 0; leave out stop to go to the end. A third number is a step: word[::2] takes every second character and word[::-1] walks backwards, which reverses the string.

بە word[start:stop] دەدات کە لە startەوە دەست پێدەکەن تا stop، بەڵام خودی stop تێدا نییە. بۆیە word[1:4] 1، 2 و 3 وەردەگرێت (وێنەکەی سەرەوە ببینە). ئەگەر start نەنووسیت لە 0ەوە دەست پێدەکات؛ ئەگەر stop نەنووسیت تا کۆتایی دەڕوات. ژمارەی سێیەم هەنگاوە: word[::2] وەردەگرێت و یەکێک جێدەهێڵێت (یەکەم، سێیەم، پێنجەم…) و word[::-1] بە پێچەوانەوە دەڕوات، واتە هەڵدەگەڕێنێتەوە.

Slices of "PYTHON"لەتکردنی "PYTHON"slices.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
word = "PYTHON"
print(word[1:4])
print(word[:2])
print(word[2:])
print(word[-3:])
print(word[:2] + word[2:])
print(word[4:42])
print(word[42:] == "")
print(word[::2])
print(word[::-1])
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Indexes 1, 2, 3. Index 4 is the stop and is left out.

1، 2 و 3. 4 کۆتاییە و دەرەوە دەمێنێتەوە.

Lineدێڕی 6

word[:i] + word[i:] always gives the whole string back.

word[:i] + word[i:] هەمیشە هەموو دەداتەوە.

Lineدێڕی 7-8

Slices never raise IndexError: a stop past the end is cut to the end, and a start past the end gives the empty string.

هەرگیز IndexError نادات: کۆتاییەک کە لە دوای بێت تا کۆتایی کورت دەکرێتەوە و دەستپێکێک لە دوای کۆتایی بەتاڵ دەدات.

Lineدێڕی 10

A step of -1 walks from the end to the start.

هەنگاوی -1 لە کۆتاییەوە بەرەو سەرەتا دەڕوات.

Quick checkپشکنینی خێراWhat does "Hawler"[1:4] give?"Hawler"[1:4] چی دەدات؟

String indexing in Python is easy ✂️Bro Code · Englishئینگلیزی · 154K viewsبینین · 3.4K likesلایک

Taking characters and parts of a string with [start:end:step], negative indexes, and reversing a string with [::-1].

وەرگرتنی و پارچەکانی بە [start:end:step]، و پێچەوانەکردنەوەی بە [::-1].

Whole video (7:27). Bro calls all of this "indexing"; it covers single indexes, slices, negative indexes and steps.

هەموو ڤیدیۆکە ⁦(7:27). Bro⁩ هەموو ئەمانە ناو دەنێت indexing: تاکە ، ، و هەنگاو.

Strings cannot be changedزنجیرەنووسەکان ناگۆڕدرێن

Strings are immutable: once a string exists, none of its characters can be changed. Trying to put a new character at an index gives a TypeError.

: کاتێک دروست بوو، هیچ ناگۆڕدرێت. هەوڵدان بۆ دانانی نوێ لە TypeError دەدات.

change.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
word = "Python"
word[0] = "J"

Instead, build a new string from pieces of the old one and store it. The old string is not touched.

لەبری ئەوە، نوێ لە پارچەکانی کۆنەکە دروست بکە و هەڵیبگرە. دەستی لێ نادرێت.

new_string.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
word = "Python"
new_word = "J" + word[1:]
print(new_word)
print(word)

This is also why string methods never change the string they belong to: they give back a new string (next screens).

ئەمە هۆکاری ئەوەشە کە هەرگیز ئەو ناگۆڕن کە بانگیان دەکات: نوێ دەگەڕێننەوە (لە شاشەکانی دواتردا).

Quick checkپشکنینی خێراname = "dilan". Which line makes name start with a capital D?name = "dilan". کام دێڕ وا دەکات name بە D-ی گەورە دەست پێ بکات؟

Joining, repeating and searching with inپێکەوەلکاندن، دووبارەکردنەوە و گەڕان بە in

You already met two operators on strings in Operators and expressions: + joins two strings (concatenation) and * repeats a string. Remember that + joins strings only: a number must first become text with str() (or use an f-string).

لە کارپێکەرەکان و دەربڕینەکان دوو ناسی: + دوو بەیەکەوە دەلکێنێت () و * دووبارە دەکاتەوە (). لەبیرت بێت + تەنها دەلکێنێت: ژمارە سەرەتا دەبێت بە str() بکرێت بە دەق (یان f-string بەکار بهێنە).

A third operator belongs to strings: in asks whether one string appears inside another, and not in asks the opposite. The answer is True or False, so it fits straight into an if. Like ==, it is case-sensitive.

سێیەم سەر بە : in دەپرسێت ئایا لە ناو تردا هەیە () و not in پێچەوانەکەی دەپرسێت. وەڵامەکە True یان Falseە، بۆیە ڕاستەوخۆ لە ناو ifـدا جێی دەبێتەوە. وەک ==، .

+, * and in+، * و inoperators.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
first = "Shilan"
last = "Aram"
print(first + " " + last)
print("-" * 20)
print("lan" in "Shilan")
print("x" in "Shilan")
print("Z" not in "Shilan")
print("shi" in "Shilan")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

+ adds no space by itself: put " " in between.

+ خۆی زیاد ناکات: " " بخەرە نێوانیان.

Lineدێڕی 5-7

in looks for the whole piece of text anywhere inside the string.

in بۆ هەموو پارچە دەقەکە لە هەر شوێنێکی ناو دەگەڕێت.

Lineدێڕی 8

"shi" is not inside "Shilan": small s is not capital S.

"shi" لە ناو "Shilan"دا نییە: sی بچووک Sی گەورە نییە.

Quick checkپشکنینی خێراWhat does "Kurd" in "Kurdistan" give, and what does "kurd" in "Kurdistan" give?"Kurd" in "Kurdistan" چی دەدات و "kurd" in "Kurdistan" چی دەدات؟

String methodsمیتۆدەکانی زنجیرەنووسە

A method is a function that belongs to a value. You call it with a dot after the value: text.upper(). String methods never change the original string (it is immutable); they return a new value, which you print or store.

کە سەر بە نرخێکە. بە خاڵێک لە دوای نرخەکەوە بانگی دەکەیت: text.upper(). هەرگیز ناگۆڕن (چونکە )؛ نرخێکی نوێ دەگەڕێننەوە کە چاپی دەکەیت یان هەڵیدەگریت.

Useful string methods
MethodWhat it gives backچی دەگەڕێنێتەوەExampleنموونە
upper()A copy in capital lettersکۆپییەک بە پیتی گەورە"ok".upper() → "OK"
lower()A copy in small lettersکۆپییەک بە پیتی بچووک"OK".lower() → "ok"
strip()A copy without spaces at the start and the endکۆپییەک بەبێ لە سەرەتا و کۆتاییدا" hi ".strip() → "hi"
replace(old, new)A copy with every old changed to newکۆپییەک کە هەموو oldـێکی تێدا کراوە بە new"tea".replace("t", "s") → "sea"
find(text)The first index of text, or -1 if it is not thereیەکەم text، یان -1 ئەگەر تێیدا نەبێت"Erbil".find("b") → 2
count(text)How many times text appearstext چەند جار دەردەکەوێت"banana".count("a") → 3
isdigit()True if every character is a digitTrue ئەگەر هەموو بن"2026".isdigit() → True
startswith(text)True if the string begins with text (endswith checks the end)True ئەگەر بە text دەست پێ بکات (endswith کۆتایی دەپشکنێت)"app.py".endswith(".py") → True
Methods at workمیتۆدەکان لە کارداmethods.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
text = "  Hello, Kurdistan!  "
print(text.strip())
print(text.upper())
print(text.lower())
print(text.strip().replace("Hello", "Hi"))
print(text.find("Kurd"))
print(text.find("Erbil"))
print(text.count("l"))
print("2026".isdigit(), "20a6".isdigit())
print("python.py".endswith(".py"))
print(text)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 5

Methods can be chained: strip() returns a new string, and replace() is called on that new string.

دەکرێت بە زنجیر بانگ بکرێن: strip() نوێ دەگەڕێنێتەوە و replace() لەسەر ئەو نوێیە بانگ دەکرێت.

Lineدێڕی 6-7

"Kurd" starts at index 9 (count the two spaces). "Erbil" is not there, so the answer is -1.

"Kurd" لە 9ەوە دەست پێدەکات (دوو بژمێرە). "Erbil" تێدا نییە، بۆیە وەڵامەکە -1ە.

Lineدێڕی 11

The original text still has its spaces: no method changed it.

textی ڕەسەن هێشتا هەیە: هیچ نەیگۆڕیوە.

Quick checkپشکنینی خێراs = " Aram ". What does len(s.strip()) give?s = " Aram ". len(s.strip()) چی دەدات؟

String methods in Python are easy! 〰️Bro Code · Englishئینگلیزی · 299K viewsبینین · 6.3K likesلایک · Partبەشی 0:00–8:05 (ofلە 12:05)

Useful string methods: len(), find(), capitalize(), upper(), lower(), isdigit(), isalpha(), count() and replace(), each returning a new value.

بەسوودی : len()، find()، capitalize()، upper()، lower()، isdigit()، isalpha()، count() و replace()؛ هەر یەکێکیان نوێ دەگەڕێنێتەوە.

Plays 0:00-8:05, the string methods; stops before the username exercise.

لە ⁦0:00⁩ تا ⁦8:05⁩ پەخش دەبێت: ؛ پێش ڕاهێنانی ڕادەوەستێت.

String methodsمیتۆدەکانی زنجیرەنووسە

More case methods, and trimming one sideمیتۆدەکانی تری گەورە و بچووکی پیت و لابردن لە یەک لا

Besides upper() and lower(), three more methods change capital and small letters. title() gives every word a capital first letter; capitalize() gives only the first character of the whole string a capital and makes the rest small; swapcase() turns capitals into small letters and small letters into capitals. strip() also has two one-sided versions: lstrip() removes spaces only at the start (l = left) and rstrip() only at the end (r = right). Kurdish letters have no capitals, so the case methods leave them unchanged.

جگە لە upper() و lower()، سێ تر پیتی گەورە و بچووک دەگۆڕن. title() یەکەم پیتی هەموو وشەیەک گەورە دەکات؛ capitalize() تەنها یەکەم هەموو گەورە دەکات و باقییەکەی بچووک دەکات؛ swapcase() پیتە گەورەکان دەکات بە بچووک و بچووکەکان بە گەورە. strip()ـیش دوو وەشانی یەک لایەنەی هەیە: lstrip() تەنها سەرەتا لادەبات (⁦l = left⁩، چونکە سەرەتای لە کۆددا لای چەپە) و rstrip() تەنها هی کۆتایی ⁦(r = right)⁩. پیتە کوردییەکان گەورە و بچووکیان نییە، بۆیە گەورە و بچووککردن وەک خۆیان دەیانهێڵنەوە.

Case methods and one-sided stripمیتۆدەکانی گەورە و بچووکی پیت و stripـی یەک لایەنەPYTHONType in a terminalلە تێرمیناڵدا بینووسە
name = "hana aram"
print(name.title())
print(name.capitalize())
print("Python IS Fun".swapcase())
print("SHILAN".capitalize())
text = "  tea  "
print("[" + text.lstrip() + "]")
print("[" + text.rstrip() + "]")
print("[" + text.strip() + "]")
print("ئارام".upper())
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Every word starts with a capital.

هەموو وشەیەک بە پیتی گەورە دەست پێدەکات.

Lineدێڕی 3

Only the first character of the string; the rest becomes small.

تەنها یەکەم ؛ باقییەکەی بچووک دەبێت.

Lineدێڕی 7-9

The brackets show where spaces are left.

کەوانەکان پیشان دەدەن لە کوێ ماونەتەوە.

Lineدێڕی 10

No capitals in Kurdish: nothing changes.

لە کوردیدا پیتی گەورە نییە: هیچ ناگۆڕێت.

Quick checkپشکنینی خێراWhat does "kurdistan region".title() give?"kurdistan region".title() چی دەدات؟

String methodsمیتۆدەکانی زنجیرەنووسە

Yes-or-no questions: the is…() methodsپرسیاری بەڵێ یان نا: میتۆدەکانی is…()

Methods whose names start with is change nothing: they answer a yes-or-no question about the whole string with True or False. Use them to check input before you convert it, for example if text.isdigit(): before int(text). Two traps: a space is neither a letter nor a digit, so "Room 12".isalnum() is False; and the empty string "" gives False for all of them.

هەر کە ناوەکەی بە is دەست پێ بکات هیچ ناگۆڕێت: وەڵامی پرسیارێکی بەڵێ یان نا دەربارەی هەموو بە True یان False دەداتەوە. بەکاریان بهێنە بۆ پشکنینی پێش گۆڕینی، بۆ نموونە if text.isdigit(): پێش int(text). دوو تەڵە: نە پیتە نە ، بۆیە "Room 12".isalnum() Falseە؛ هەروەها بەتاڵ "" بۆ هەموویان False دەدات.

The is…() methods (each returns True or False) is…() (هەر یەکێکیان True یان False دەگەڕێنێتەوە)
MethodTrue when…Trueە کاتێک…Exampleنموونە
isalpha()every character is a letter (Kurdish letters too)هەموو پیت بن (پیتی کوردیش)"Hawler".isalpha() → True
isdigit()every character is a digitهەموو بن"2026".isdigit() → True
isalnum()every character is a letter or a digitهەموو پیت یان بن"Room12".isalnum() → True
isspace()every character is white space (space, tab, new line)هەموو بن (، Tab، دێڕی نوێ)" ".isspace() → True
islower()it has letters, and all of them are smallپیتی تێدا بێت و هەموویان بچووک بن"erbil".islower() → True
isupper()it has letters, and all of them are capitalsپیتی تێدا بێت و هەموویان گەورە بن"IT".isupper() → True
istitle()every word starts with a capital and goes on in small lettersهەموو وشەیەک بە پیتی گەورە دەست پێ بکات و بە بچووک بەردەوام بێت"Hana Aram".istitle() → True
PYTHONType in a terminalلە تێرمیناڵدا بینووسە
print("Hawler".isalpha(), "Hawler City".isalpha())
print("2026".isdigit(), "20.5".isdigit())
print("Room12".isalnum(), "Room 12".isalnum())
print("   ".isspace(), "".isspace())
print("erbil".islower(), "Erbil".islower())
print("IT".isupper(), "It".isupper())
print("Hana Aram".istitle(), "Hana aram".istitle())
print("ئارام".isalpha())
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

The space makes the second answer False.

وەڵامی دووەم دەکات بە False.

Lineدێڕی 2

The dot is not a digit.

خاڵەکە نییە.

Lineدێڕی 4

The empty string is False for every is…() method.

بەتاڵ بۆ هەموو is…() Falseە.

Lineدێڕی 8

Kurdish letters are letters too.

پیتە کوردییەکانیش پیتن.

Quick checkپشکنینی خێراWhat does "Duhok City".isalpha() give?"Duhok City".isalpha() چی دەدات؟

String methodsمیتۆدەکانی زنجیرەنووسە

find() or index()? Searching part of a stringfind() یان index()؟ گەڕان لە بەشێکی زنجیرەنووسەیەکدا

find(text) gives the first index of text, or -1 when it is missing. index(text) searches in the same way, but when text is missing it stops the program with a ValueError — use it only when the text must be there. rfind() searches from the end (r = right) and gives the last index. find(), index() and count() also accept two optional numbers, start and end: then they look only inside s[start:end], but the index they return still counts from the start of the whole string.

find(text) یەکەم text دەدات، یان -1 ئەگەر تێدا نەبێت. index(text) بە هەمان شێوە دەگەڕێت، بەڵام ئەگەر text تێدا نەبێت بە ValueError دەوەستێنێت — تەنها کاتێک بەکاری بهێنە کە دەقەکە دەبێت هەبێت. rfind() لە کۆتاییەوە دەگەڕێت ⁦(r = right)⁩ و دوایین دەدات. find()، index() و count() دوو ژمارەی ئارەزوومەندانەش وەردەگرن، start و end: ئەوکات تەنها لە ناو s[start:end]ـدا دەگەڕێن، بەڵام ئەو دەیگەڕێننەوە هەر لە سەرەتای هەموو دەژمێردرێت.

PYTHONType in a terminalلە تێرمیناڵدا بینووسە
city = "Sulaimani"
print(city.find("a"))
print(city.rfind("a"))
print(city.find("a", 4))
print(city.count("a"))
print(city.count("a", 0, 4))
print(city.find("x"))
print(city.index("a"))
print(city.index("x"))
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

Indexes: S 0, u 1, l 2, a 3, i 4, m 5, a 6, n 7, i 8.

: ⁦S 0⁩، ⁦u 1⁩، ⁦l 2⁩، ⁦a 3⁩، ⁦i 4⁩، ⁦m 5⁩، ⁦a 6⁩، ⁦n 7⁩، ⁦i 8⁩.

Lineدێڕی 4

The search starts at index 4, so it finds the second a.

گەڕان لە 4ەوە دەست پێدەکات، بۆیە aی دووەم دەدۆزێتەوە.

Lineدێڕی 6

Counts only in city[0:4], which is "Sula".

تەنها لە city[0:4]ـدا دەژمێرێت، کە "Sula"ـیە.

Lineدێڕی 7-9

Missing text: find() gives -1, index() stops with a ValueError.

دەقی نەبوو: find() -1 دەدات و index() بە ValueError دەوەستێت.

Quick checkپشکنینی خێراWhat does "banana".find("n", 3) give?"banana".find("n", 3) چی دەدات؟

String methodsمیتۆدەکانی زنجیرەنووسە

split() and join()split() و join()

split() cuts a string into a list of smaller strings. With no argument it cuts at spaces, and a run of several spaces counts as one. With an argument it cuts at that text, for example "-". join() does the opposite: ", ".join(items) glues a list of strings together with ", " between them. (Lists are taught in Lists.) Later, split() cuts every line of a text file into words: Words, numbers and CSV tables.

split() دەکات بە لە بچووکتر. بەبێ لە دەیبڕێت و چەند لە دوای یەک وەک یەک دەژمێردرێن. بە لەو دەقەدا دەیبڕێت، بۆ نموونە "-". join() پێچەوانەکەی دەکات: ", ".join(items) لە پێکەوە دەلکێنێت و ", " دەخاتە نێوانیانەوە. ( لە لیستەکان دەخوێنرێن.) دواتر split() هەر دێڕێکی دەکات بە وشە: وشە، ژمارە و خشتەی CSV.

split_join.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
line = "tea  coffee   juice"
drinks = line.split()
print(drinks)
print(len(drinks))
date = "2026-10-22"
print(date.split("-"))
print(", ".join(drinks))
print("-".join(["2026", "10", "22"]))
print("-".join("Hello"))
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 9

A string is a sequence of characters, so join() puts - between its letters.

بریتییە لە لە ، بۆیە join() - دەخاتە نێوان پیتەکانییەوە.

Quick checkپشکنینی خێراWhat does "a,b,,c".split(",") give?"a,b,,c".split(",") چی دەدات؟

String methodsمیتۆدەکانی زنجیرەنووسە

partition(): cut once, at the first separatorpartition(): یەکجار ببڕە، لە یەکەم جیاکەرەوەدا

split() cuts at every separator. Sometimes you want only one cut, at the first one — for example in "name: Hana Aram". partition(sep) does exactly that and always returns three pieces: the text before sep, sep itself and the text after it. If sep is missing, you get the whole string and two empty strings. The three pieces come in a tuple, a fixed group of values (see Tuples, sets and dictionaries); you read them by index, like the characters of a string.

split() لە هەموو جیاکەرەوەیەکدا دەبڕێت. هەندێک جار تەنها یەک بڕینت دەوێت، لە یەکەمیاندا — بۆ نموونە لە "name: Hana Aram"ـدا. partition(sep) ڕێک ئەوە دەکات و هەمیشە سێ پارچە دەگەڕێنێتەوە: دەقی پێش sep، خودی sep و دەقی دوای ئەو. ئەگەر sep تێدا نەبێت، هەموو و دوو بەتاڵ وەردەگریت. سێ پارچەکە لە ، کۆمەڵێک نرخی جێگیر (بڕوانە تاپڵەکان، کۆمەڵەکان و فەرهەنگەکان)؛ وەک بە دەیانخوێنیتەوە.

PYTHONType in a terminalلە تێرمیناڵدا بینووسە
line = "name: Hana Aram"
parts = line.partition(":")
print(parts)
print(parts[0])
print(parts[2].strip())
print("time: 10:30".partition(":"))
print("no colon here".partition(":"))
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-3

Three pieces: before, the separator, after.

سێ پارچە: پێش، جیاکەرەوەکە، دوای.

Lineدێڕی 5

strip() removes the space after the colon.

strip() دوای دوو خاڵەکە لادەبات.

Lineدێڕی 6

Only the first : cuts; the second stays in the last piece.

تەنها یەکەم : دەبڕێت؛ دووەمیان لە پارچەی کۆتاییدا دەمێنێتەوە.

Lineدێڕی 7

No : at all: the whole string and two empty strings.

هیچ :ـێک نییە: هەموو و دوو بەتاڵ.

Quick checkپشکنینی خێراWhat does "a=b=c".partition("=") give?"a=b=c".partition("=") چی دەدات؟

Comparing stringsبەراوردکردنی زنجیرەنووسەکان

The comparison operators ==, !=, <, >, <= and >= work on strings too. == is True only when both strings have exactly the same characters in the same order — a capital and a small letter are different characters.

==، !=، <، >، <= و >= لەسەر کار دەکەن. == تەنها کاتێک Trueە کە هەردوو ڕێک هەمان بە هەمان ڕیز هەبێت — پیتی گەورە و بچووک دوو جیاوازن.

< and > follow lexicographic order (dictionary order). Python compares the first characters of both strings. If they are the same, it moves to the second pair, and so on. The first pair that differs decides the answer; the rest is never looked at. If one string runs out first because it is the beginning of the other, the shorter one is smaller. The empty string "" is smaller than every other string.

< و > پەیڕەو دەکەن. یەکەم هەردوو بەراورد دەکات. ئەگەر وەک یەک بن، دەچێتە سەر جووتی دووەم و بەم شێوەیە. یەکەم جووت کە جیاواز بێت وەڵامەکە دیاری دەکات؛ ئەوانی تر هەرگیز سەیر ناکرێن. ئەگەر زووتر تەواو بێت چونکە سەرەتای ئەوی ترە، کورتەکە بچووکترە. بەتاڵ "" لە هەموو تر بچووکترە.

Animation: "Dilan" and "Dilshad" in letter boxes, one above the other. A pointer moves along the pairs: D = D, i = i, l = l, all the same. At the fourth pair a and s differ; ord("a") = 97 is less than ord("s") = 115, so "Dilan" < "Dilshad" is True and the remaining letters are never compared.
Animation: "Dilan" and "Dilshad" in letter boxes, one above the other. A pointer moves along the pairs: D = D, i = i, l = l, all the same. At the fourth pair a and s differ; ord("a") = 97 is less than ord("s") = 115, so "Dilan" < "Dilshad" is True and the remaining letters are never compared.
The first pair of different letters decides.یەکەم جووتی پیتی جیاواز بڕیار دەدات.CêIT
Six operators, eight comparisonsشەش کارپێکەر، هەشت بەراوردcompare.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
print("Dilan" == "Dilan")
print("Dilan" == "dilan")
print("Dilan" != "Dilshad")
print("Dilan" < "Dilshad")
print("tea" < "teacher")
print("" < "a")
print("Soran" >= "Shilan")
print("Hana" <= "Hana")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

D and d are different characters, so the strings are not equal.

D و d دوو جیاوازن، بۆیە یەکسان نین.

Lineدێڕی 5

"tea" is the beginning of "teacher", so the shorter string is smaller.

"tea" سەرەتای "teacher"ە، بۆیە بچووکترە.

Lineدێڕی 7

S = S, then o is greater than h, so "Soran" is greater.

S = S، پاشان o لە h گەورەترە، بۆیە "Soran" گەورەترە.

Quick checkپشکنینی خێراIs "Karwan" < "Karim" True or False?ئایا "Karwan" < "Karim" Trueە یان False؟

Comparing stringsبەراوردکردنی زنجیرەنووسەکان

Why is "A" < "a" True?بۆچی "⁦A" < "a⁩" ڕاستە؟

A note from the course shows four surprising results: "A" < "a", "S" < "e", "S" < "eggs" and "Spam" < "eggs" are all True. Why would a capital S be smaller than a small e? Because Python does not compare letters as a person does. Every character has a number, its code point, and Python compares those numbers. ord() gives a character's number and chr() turns a number back into a character.

تێبینییەکی کۆرسەکە چوار ئەنجامی سەرسوڕهێنەر پیشان دەدات: "A" < "a"، "S" < "e"، "S" < "eggs" و "Spam" < "eggs" هەموویان Trueن. بۆچی S-ی گەورە لە e-ی بچووک بچووکترە؟ چونکە پیتەکان وەک مرۆڤ بەراورد ناکات. هەر ژمارەیەکی هەیە کە پێی دەوترێت و ئەو ژمارانە بەراورد دەکات. ord() ژمارەی دەدات و chr() ژمارەیەک دەکاتەوە بە .

The note's four comparisons, checkedچوار بەراوردەکەی تێبینییەکە، پشکنراوowner_note.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
print("A" < "a")
print("S" < "e")
print("S" < "eggs")
print("Spam" < "eggs")
print(ord("A"), ord("a"))
print(ord("S"), ord("e"))
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

Only the first pair is compared: S (83) < e (101). The rest of both words is never looked at.

تەنها یەکەم جووت بەراورد دەکرێت: S ⁦(83)⁩ < e ⁦(101)⁩. باقیی هەردوو وشەکە هەرگیز سەیر ناکرێت.

Some code points: every capital English letter comes before every small oneهەندێک : هەموو پیتە گەورە ئینگلیزییەکان پێش هەموو پیتە بچووکەکان دێن
CharactersCode points
A … Z65 … 90
a … z97 … 122
0 … 948 … 57
space32
ئ ا ب1574 1575 1576
Quick checkپشکنینی خێراWhy is "Zebra" < "apple" True?بۆچی "Zebra" < "apple" ڕاستە؟

Comparing stringsبەراوردکردنی زنجیرەنووسەکان

Kurdish letters in comparisonsپیتە کوردییەکان لە بەراورددا

Kurdish letters have large code points (from about 1570 up), so any word that starts with a Kurdish letter is "greater" than any English word: "ئارام" > "Zana" is True. There is a second surprise: the code points of Kurdish letters do not follow the order of the Kurdish alphabet. پ comes before ت in the alphabet, but its number is larger (1662>15781662 > 1578), so Python puts "پیاو" after "تەنها".

پیتە کوردییەکان گەورەیان هەیە (نزیکەی لە ⁦1570⁩ بەرەو سەر)، بۆیە هەر وشەیەک کە بە پیتێکی کوردی دەست پێ بکات لە هەموو وشەیەکی ئینگلیزی «گەورەترە»: "ئارام" > "Zana" Trueە. سەرسوڕمانێکی دووەمیش هەیە: پیتە کوردییەکان ڕیزبەندیی ئەلفوبێی کوردی پەیڕەو ناکەن. پ لە ئەلفوبێدا پێش ت دێت، بەڵام ژمارەکەی گەورەترە (⁦1662⁩ لە ⁦1578⁩ گەورەترە)، بۆیە "پیاو" دەخاتە دوای "تەنها".

kurdish_compare.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
print(ord("ئ"), ord("ا"), ord("ب"))
print("ئارام" > "Zana")
print(ord("پ"), ord("ت"))
print("پیاو" < "تەنها")
Quick checkپشکنینی خێراIs "باخ" > "Zoo" True?ئایا "باخ" > "Zoo" Trueە؟

Comparing stringsبەراوردکردنی زنجیرەنووسەکان

Comparing without caring about capitalsبەراوردکردن بەبێ گرنگیدان بە پیتی گەورە

Because of code points, "apple" < "Banana" is False: B (66) is smaller than a (97). To compare the way a person reads, change both strings to the same case first, usually with .lower(). The same trick checks a user's answer: answer.strip().lower() == "yes" accepts "YES", "Yes " and "yes". casefold() is a stronger lower() for some languages (German ß becomes ss).

بەهۆی "apple" < "Banana" Falseە: B ⁦(66)⁩ لە a ⁦(97)⁩ بچووکترە. بۆ ئەوەی وەک مرۆڤێک بەراورد بکەیت، سەرەتا هەردوو بکە بە یەک جۆری پیت، زۆربەی جار بە .lower(). هەمان فێڵ وەڵامی دەپشکنێت: answer.strip().lower() == "yes" "YES"، "Yes " و "yes" قبووڵ دەکات. casefold() lower()ـێکی بەهێزترە بۆ هەندێک زمان (ßی ئەڵمانی دەبێت بە ss).

ignore_case.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
print("apple" < "Banana")
print("apple".lower() < "Banana".lower())
answer = "YES"
print(answer == "yes")
print(answer.lower() == "yes")
print("Straße".casefold() == "STRASSE".casefold())
Quick checkپشکنینی خێراA program asks "Continue?" and the user types Yes. Which test accepts it? دەپرسێت "⁦Continue?⁩" و Yes دەنووسێت. کام پشکنین قبووڵی دەکات؟

Common mistakesهەڵە باوەکان

Mistakes with strings and their fixesهەڵەکان لەگەڵ و چارەسەرەکانیان
MistakeهەڵەWhat happensچی ڕوودەداتFixچارەسەر
word[len(word)]IndexError: the last index is one less than the lengthIndexError: دوایین یەکێک لە درێژییەکە کەمترەword[len(word) - 1] / word[-1]
word[0] = "J"TypeError: strings are immutableTypeError: word = "J" + word[1:]
name.upper()Nothing visible: the new string is thrown awayهیچ شتێکی بینراو: فڕێ دەدرێتname = name.upper()
"Age: " + 19TypeError: + cannot join a string and a numberTypeError: + ناتوانێت و ژمارەیەک بلکێنێت"Age: " + str(19)
"YES" == "yes"False: comparisons are case-sensitiveFalse: بەراوردەکان answer.lower() == "yes"
"10" < "9"True: digits in text are compared as characters, 1 before 9True: ژمارەکانی ناو دەق وەک بەراورد دەکرێن، 1 پێش 9int("10") < int("9")
print(“Hi”)SyntaxError: curly quotes from a word processor are not Python quotesSyntaxError: چەماوەی پرۆگرامی نووسین نینprint("Hi")
Numbers stored as text compare like wordsژمارەی هەڵگیراو وەک دەق وەک وشە بەراورد دەکرێتnumbers_as_text.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
print("10" < "9")
print(10 < 9)
print(int("10") < int("9"))

Curly quotes are shown with a full example in The for loop and range().

چەماوە بە نموونەیەکی تەواو لە خولی for و range() پیشان دراون.

Exercisesڕاهێنانەکان

ExerciseڕاهێنانInitialsپیتە سەرەتاییەکان

Ask for a full name (first and last name) and print the initials in capitals, like S.A. for shilan aram.

داوای ناوی تەواو بکە (ناوی یەکەم و دووەم) و پیتە سەرەتاییەکان بە پیتی گەورە چاپ بکە، وەک S.A. بۆ shilan aram.

Hintئاماژە

split() gives the two names; [0] gives the first letter of each.

split() دوو ناوەکە دەدات؛ [0] یەکەم پیتی هەر یەکێکیان دەدات.

Show answerوەڵامەکە پیشان بدە
initials.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
full_name = input("Your full name: ")
parts = full_name.split()
initials = parts[0][0] + "." + parts[1][0] + "."
print("Initials:", initials.upper())
ExerciseڕاهێنانSame forwards and backwardsوەک یەک لە هەردوو ئاڕاستەوە

A word like level reads the same backwards. Ask for a word and print True if it does, False if not. Capital letters should not matter (Level counts).

وشەیەکی وەک level لە پێچەوانەشەوە هەمان شت دەخوێنرێتەوە. داوای وشەیەک بکە و ئەگەر وا بوو True چاپ بکە، ئەگەر نا False. پیتی گەورە نابێت گرنگ بێت (Levelیش حساب دەکرێت).

Hintئاماژە

word[::-1] is the reversed word.

word[::-1] هەمان وشەیە بە پێچەوانەوە.

Show answerوەڵامەکە پیشان بدە
same_backwards.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
word = input("A word: ").strip().lower()
print(word == word[::-1])
A second runجێبەجێکردنی دووەمsame_backwards.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
word = input("A word: ").strip().lower()
print(word == word[::-1])
ExerciseڕاهێنانPredict the comparisonsئەنجامی بەراوردەکان پێشبینی بکە

Without running it, write True or False for each line. Then run it and explain every answer.

بەبێ جێبەجێکردن، بۆ هەر دێڕێک True یان False بنووسە. پاشان جێبەجێی بکە و هەموو وەڵامێک ڕوون بکەرەوە.

predict.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
print("Rezan" == "rezan")
print("cat" < "car")
print("book" < "bookshop")
print("Z" < "a")
print("Hana" > "Hanar")
print("99" < "100")
Show answerوەڵامەکە پیشان بدە
predict.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
print("Rezan" == "rezan")
print("cat" < "car")
print("book" < "bookshop")
print("Z" < "a")
print("Hana" > "Hanar")
print("99" < "100")
  • R and r are different characters.
  • c, a are the same; then t (116) is greater than r (114).
  • "book" is the beginning of "bookshop", so it is smaller.
  • Z is 90 and a is 97.
  • "Hana" is the beginning of "Hanar", so it is smaller, not greater.
  • Text, not numbers: 9 is greater than 1, so "99" is greater.
  • R و r دوو جیاوازن.
  • c و a وەک یەکن؛ پاشان t ⁦(116)⁩ لە r ⁦(114)⁩ گەورەترە.
  • "book" سەرەتای "bookshop"ە، بۆیە بچووکترە.
  • Z ⁦90⁩ە و a ⁦97⁩.
  • "Hana" سەرەتای "Hanar"ە، بۆیە بچووکترە نەک گەورەتر.
  • دەقە نەک ژمارە: 9 لە 1 گەورەترە، بۆیە "99" گەورەترە.
ExerciseڕاهێنانFile namesناوی فایل

file_name = "report.final.py". Print the part before the first dot, whether the name ends with .py, and how many dots it has.

file_name = "report.final.py". ئەو بەشەی پێش یەکەم خاڵ چاپ بکە، ئایا ناوەکە بە .py کۆتایی دێت و چەند خاڵی تێدایە.

Show answerوەڵامەکە پیشان بدە
file_name.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
file_name = "report.final.py"
dot = file_name.find(".")
print(file_name[:dot])
print(file_name.endswith(".py"))
print(file_name.count("."))
ExerciseڕاهێنانPredict: slices and methodsپێشبینی بکە: لەتکردن و میتۆدەکان

Without running it, write down what each line prints. Then run it and check. Careful with the last line.

بەبێ جێبەجێکردن، بنووسە هەر دێڕێک چی چاپ دەکات. پاشان جێبەجێی بکە و بیپشکنە. ئاگاداری دوایین دێڕ بە.

PYTHONType in a terminalلە تێرمیناڵدا بینووسە
s = "Kurdistan Region"
print(s[-6:-1])
print(s[::2])
print(s[-1])
print(2 * s[:4])
print(s[::-2])
print(s[:3] + s[3:] == s)
print(s.swapcase())
print(s.startswith("Kurd"))
print(s.isalpha())
Show answerوەڵامەکە پیشان بدە
PYTHONType in a terminalلە تێرمیناڵدا بینووسە
s = "Kurdistan Region"
print(s[-6:-1])
print(s[::2])
print(s[-1])
print(2 * s[:4])
print(s[::-2])
print(s[:3] + s[3:] == s)
print(s.swapcase())
print(s.startswith("Kurd"))
print(s.isalpha())
  • s[-6:-1]: index -6 is R, and -1 (the last n) is the stop, so it is left out.
  • s[::2]: every second character from index 0 — the space at index 9 is skipped.
  • 2 * s[:4] repeats "Kurd" twice.
  • s[::-2] walks backwards from the last character and takes every second one.
  • s[:i] + s[i:] is always the whole string, so the comparison is True.
  • isalpha() is False because of the space: a space is not a letter.
  • s[-6:-1]: -6 یەکسانە بە R و -1 (دوایین n) کۆتاییە، بۆیە تێدا نییە.
  • s[::2]: لە 0ەوە وەردەگرێت و یەکێک جێدەهێڵێت — 9 جێدەهێڵرێت.
  • 2 * s[:4] "Kurd" دوو جار دووبارە دەکاتەوە.
  • s[::-2] لە دوایین بەرەو دواوە دەڕوات و وەردەگرێت و یەکێک جێدەهێڵێت.
  • s[:i] + s[i:] هەمیشە هەموو ، بۆیە بەراوردەکە Trueە.
  • isalpha() بەهۆی Falseە: پیت نییە.
ExerciseڕاهێنانPredict: a home addressپێشبینی بکە: ناونیشانی ماڵێک

Predict each line, then run the program. Why does it stop at the last line, and which method would give -1 instead?

هەر دێڕێک پێشبینی بکە، پاشان جێبەجێ بکە. بۆچی لە دوایین دێڕدا دەوەستێت و کام لەبری ئەوە -1 دەدات؟

PYTHONType in a terminalلە تێرمیناڵدا بینووسە
address = "Street 60, Erbil, Erbil Governorate"
print(address.upper())
print(address.count("Erbil"))
print(address.find("Erbil"), address.rfind("Erbil"))
print(address.split(","))
print(address.partition(","))
print(address.replace("Street", "St."))
print(address.index("Duhok"))
Show answerوەڵامەکە پیشان بدە
PYTHONType in a terminalلە تێرمیناڵدا بینووسە
address = "Street 60, Erbil, Erbil Governorate"
print(address.upper())
print(address.count("Erbil"))
print(address.find("Erbil"), address.rfind("Erbil"))
print(address.split(","))
print(address.partition(","))
print(address.replace("Street", "St."))
print(address.index("Duhok"))
  • find() gives the first "Erbil" (index 11), rfind() the last one (index 18).
  • split(",") keeps the space after each comma; strip() each piece if you do not want it.
  • partition(",") cuts only at the first comma.
  • "Duhok" is not in the string, so index() stops the program with a ValueError. address.find("Duhok") would give -1 and the program would go on.
  • find() یەکەم "Erbil" دەدات ( ⁦11⁩) و rfind() دوایینیان ( ⁦18⁩).
  • split(",") دوای هەر کۆمایەک دەهێڵێتەوە؛ ئەگەر نەتەوێت، strip() لەسەر هەر پارچەیەک بەکار بهێنە.
  • partition(",") تەنها لە یەکەم کۆمادا دەبڕێت.
  • "Duhok" لە نییە، بۆیە index() بە ValueError دەوەستێنێت. address.find("Duhok") -1 دەدات و بەردەوام دەبێت.
ExerciseڕاهێنانDelete a characterنووسەیەک بسڕەوە

A date is typed as 2026-10-24. Remove every - and print the result and its length. Then turn every space in "Hana Aram Karim" into -.

بەروارێک بەم شێوەیە نووسراوە: 2026-10-24. هەموو -ـێک لاببە و ئەنجامەکە و درێژییەکەی چاپ بکە. پاشان هەموو ناو "Hana Aram Karim" بکە بە -.

Hintئاماژە

replace(old, "") replaces with nothing — that deletes.

replace(old, "") بە هیچ دەیگۆڕێت — واتە دەیسڕێتەوە.

Show answerوەڵامەکە پیشان بدە
PYTHONType in a terminalلە تێرمیناڵدا بینووسە
date = "2026-10-24"
digits = date.replace("-", "")
print(digits, len(digits))
full_name = "Hana Aram Karim"
print(full_name.replace(" ", "-"))
print(date)

Each replace() returns a new string; the old one is unchanged, as the last line shows (strings are immutable).

هەر replace()ـێک نوێ دەگەڕێنێتەوە؛ کۆنەکە ناگۆڕێت، وەک دوایین دێڕ پیشانی دەدات ( ).

ExerciseڕاهێنانExplain the noteتێبینییەکە ڕوون بکەرەوە

In your own words, explain why "Spam" < "eggs" is True although s comes after e in the alphabet.

بە وشەی خۆت ڕوونی بکەرەوە بۆچی "Spam" < "eggs" Trueە، هەرچەندە s لە ئەلفوبێدا لە دوای e دێت.

Show answerوەڵامەکە پیشان بدە

Python compares the first characters, S and e, by their code points: ord("S") is 83 and ord("e") is 101. 83<10183 < 101, so the answer is True at once and the other letters are never compared. Capital letters (65–90) all come before small letters (97–122). With "Spam".lower() < "eggs".lower() the answer would be False.

یەکەم هەردوو وشەکە، S و e، بە بەراورد دەکات: ord("S") ⁦83⁩ە و ord("e") ⁦101. 83⁩ لە ⁦101⁩ کەمترە، بۆیە وەڵامەکە دەستبەجێ Trueە و پیتەکانی تر هەرگیز بەراورد ناکرێن. هەموو پیتە گەورەکان ⁦(65–90)⁩ پێش پیتە بچووکەکان ⁦(97–122)⁩ دێن. بە "Spam".lower() < "eggs".lower() وەڵامەکە False دەبوو.

More string exercises need a loop: hiding the vowels of a text, counting its letters, digits and spaces, and adding up the digits in it. They come after the loop lessons, in More exercises: text and number series.

هەندێک ڕاهێنانی تری پێویستیان بە هەیە: شاردنەوەی پیتە بزوێنەکانی دەقێک، ژماردنی پیت، و و کۆکردنەوەی ناوی. ئەوانە دوای وانەکانی دێن، لە ڕاهێنانی زیاتر: دەق و زنجیرەی ژمارە.

Summaryپوختە

  • A string is text: a sequence of characters between quotes. len() counts them.
  • Indexes start at 0; the last is len(s) - 1 or -1. A wrong index gives IndexError.
  • s[start:stop] takes a slice; stop is not included. s[::-1] reverses.
  • Strings are immutable: build a new string and store it.
  • + joins, * repeats, in searches; methods like upper(), strip(), split() return new values.
  • More methods: title(), capitalize(), swapcase(), lstrip()/rstrip(); the is…() methods answer True or False; find() gives -1 but index() raises ValueError; partition() cuts once.
  • Strings compare letter by letter by code point (ord()): capitals before small letters, Kurdish letters after English ones. Use lower() to ignore case.
  • To visit every character one by one, use a loop: see The for loop and range().
  • دەقە: لە لە نێوان . len() دەیانژمێرێت.
  • لە 0ەوە دەست پێدەکەن؛ دوایینیان len(s) - 1 یان -1ە. هەڵە IndexError دەدات.
  • s[start:stop] دەکات؛ stop تێدا نییە. s[::-1] هەڵیدەگەڕێنێتەوە.
  • : نوێ دروست بکە و هەڵیبگرە.
  • + دەلکێنێت، * دووبارە دەکاتەوە، in دەگەڕێت؛ وەک upper()، strip() و split() نرخی نوێ دەگەڕێننەوە.
  • زیاتر: title()، capitalize()، swapcase() و lstrip()/rstrip()؛ is…() وەڵامی True یان False دەدەنەوە؛ find() -1 دەدات بەڵام index() ValueError دروست دەکات؛ partition() یەکجار دەبڕێت.
  • پیت بە پیت بەپێی (ord()) بەراورد دەکرێن: گەورەکان پێش بچووکەکان و پیتە کوردییەکان دوای ئینگلیزییەکان. بۆ پشتگوێخستنی گەورە و بچووکی lower() بەکار بهێنە.
  • بۆ سەردانکردنی هەموو یەک لە دوای یەک، بەکار بهێنە: بڕوانە خولی for و range().
Review questionsپرسیارەکانی پێداچوونەوە
What is the index of the last character of a string s?ئیندێکسی دوایین نووسەی زنجیرەنووسەی s چییە؟

len(s) - 1, or simply -1.

len(s) - 1، یان بە سادەیی -1.

What does "Hello"[1:3] give?"Hello"[1:3] چی دەدات؟

"el" — indexes 1 and 2; index 3 is the stop.

"el" — 1 و 2؛ 3 کۆتاییە.

Why does s[0] = "X" fail?بۆچی s[0] = "X" سەرناکەوێت؟

Strings are immutable. Make a new string: s = "X" + s[1:].

. نوێ دروست بکە: s = "X" + s[1:].

Why is "B" < "a" True?بۆچی "B" < "a" ڕاستە؟

Python compares code points: ord("B") is 66 and ord("a") is 97.

بەراورد دەکات: ord("B") ⁦66⁩ە و ord("a") ⁦97⁩.

Videoڤیدیۆ

Watch a videoسەیری ڤیدیۆیەک بکە

A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.

Python Tutorial for Beginners 2: Strings - Working with Textual DataCorey Schafer · Englishئینگلیزی · 2.2M viewsبینین · 46K likesلایک · Partبەشی 0:00–15:01 (ofلە 21:11)

Strings from the start: quotes, len(), indexes from 0, slicing, useful methods like lower(), count(), find() and replace(), and joining strings.

لە سەرەتاوە: ، len()، لە سفرەوە، ، بەسوود وەک lower()، count()، find() و replace() و .

Plays 0:00-15:01 (quotes, indexing, slicing, methods, replace, concatenation); stops before string formatting.

لە ⁦0:00⁩ تا ⁦15:01⁩ پەخش دەبێت (، ، ، ، replace و )؛ پێش فۆرماتکردنی ڕادەوەستێت.

Lesson complete!وانەکە تەواو بوو!

Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.

Key termsزاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

The while Loopخولی while

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.