Class - XII: SEMESTER – III: Unit – 1: Python Programming: Section 9: Strings
Python Programming: Strings
১. String কী?
Python programming language-এ String হলো character-এর sequence বা ধারাবাহিক collection। অর্থাৎ, কোনো text data store করার জন্য string ব্যবহার করা হয়। একটি string-এর মধ্যে letter, digit, symbol, space — সবকিছু থাকতে পারে।
যেমন কোনো মানুষের নাম, address, email, password, message, sentence, paragraph ইত্যাদি programming-এ
string হিসেবে handle করা হয়। Python-এ string সাধারণত single quote ' ', double quote
" ", অথবা triple quote ''' ''' / """ """ দিয়ে লেখা হয়।
String-এর উদাহরণ:
name = "Rahim"
message = 'Welcome to Python'
paragraph = """Python is a powerful programming language."""
print(name)
print(message)
print(paragraph)
এখানে name, message এবং paragraph — তিনটিই string variable।
২. String Indexing
Python string-এর প্রতিটি character-এর একটি position থাকে, যাকে index বলা হয়।
Python-এ indexing শুরু হয় 0 থেকে। অর্থাৎ string-এর প্রথম character-এর index 0,
দ্বিতীয় character-এর index 1, এভাবে চলতে থাকে।
Example:
text = "Python"
print(text[0])
print(text[1])
print(text[5])
Output:
P
y
n
এখানে text[0] হলো প্রথম character P, text[1] হলো y,
এবং text[5] হলো শেষ character n।
Negative Indexing:
Python-এ negative indexing-ও ব্যবহার করা যায়। Negative indexing string-এর শেষ দিক থেকে শুরু হয়।
শেষ character-এর index -1, তার আগের character-এর index -2।
text = "Python"
print(text[-1])
print(text[-2])
print(text[-6])
Output:
n
o
P
৩. String Operations
Python-এ string-এর উপর বিভিন্ন ধরনের operation করা যায়। এর মধ্যে গুরুত্বপূর্ণ operations হলো:
- Concatenation
- Repetition
- Membership
- Slicing
৪. String Concatenation
দুই বা ততোধিক string একসাথে যুক্ত করার প্রক্রিয়াকে String Concatenation বলা হয়।
Python-এ string concatenate করার জন্য + operator ব্যবহার করা হয়।
Example:
first_name = "Rahim"
last_name = "Uddin"
full_name = first_name + " " + last_name
print(full_name)
Output:
Rahim Uddin
এখানে first_name এবং last_name string দুইটি + operator দিয়ে যুক্ত করা হয়েছে।
মাঝখানে একটি space string " " ব্যবহার করা হয়েছে যাতে output সুন্দর দেখায়।
Wrong Example:
age = 20
print("Age is " + age)
এই code error দেবে, কারণ string-এর সাথে integer সরাসরি concatenate করা যায় না।
Correct Example:
age = 20
print("Age is " + str(age))
৫. String Repetition
একই string একাধিকবার repeat করতে * operator ব্যবহার করা হয়।
এটাকে String Repetition বলা হয়।
Example:
word = "Python "
result = word * 3
print(result)
Output:
Python Python Python
এখানে word * 3 মানে string তিনবার repeat হবে।
Practical Example:
line = "-" * 30
print(line)
print("Student Result")
print(line)
এই ধরনের repetition ব্যবহার করে output formatting সুন্দর করা যায়।
৬. Membership Operation
কোনো character বা substring একটি string-এর মধ্যে আছে কিনা তা check করতে membership operators ব্যবহার করা হয়।
Python-এ membership operators হলো in এবং not in।
Example:
text = "I love Python programming"
print("Python" in text)
print("Java" in text)
print("Java" not in text)
Output:
True
False
True
এখানে "Python" string-এর মধ্যে আছে, তাই result True।
কিন্তু "Java" নেই, তাই result False।
Practical Example:
email = "student@example.com"
if "@" in email:
print("Valid email format")
else:
print("Invalid email format")
এখানে simple email validation-এর জন্য @ symbol আছে কিনা check করা হয়েছে।
৭. String Slicing
String-এর নির্দিষ্ট অংশ বের করার প্রক্রিয়াকে Slicing বলা হয়।
Python-এ slicing করার জন্য square bracket [ ] ব্যবহার করা হয়।
Syntax:
string[start : stop : step]
| Part | ব্যাখ্যা |
|---|---|
start |
কোন index থেকে শুরু হবে |
stop |
কোন index-এর আগ পর্যন্ত যাবে |
step |
কত ধাপ করে এগোবে |
Example:
text = "Python Programming"
print(text[0:6])
print(text[7:18])
print(text[:6])
print(text[7:])
print(text[::2])
Output:
Python
Programming
Python
Programming
Pto rgamn
text[0:6] index 0 থেকে শুরু করে index 6-এর আগ পর্যন্ত character নেয়।
তাই output হলো Python।
Reverse String using Slicing:
text = "Python"
reverse_text = text[::-1]
print(reverse_text)
Output:
nohtyP
৮. Traversing a String using Loops
String-এর প্রতিটি character একে একে access বা process করার প্রক্রিয়াকে traversing বলা হয়।
Python-এ string traverse করার জন্য সাধারণত for loop এবং while loop ব্যবহার করা হয়।
Using for Loop:
text = "Python"
for ch in text:
print(ch)
Output:
P
y
t
h
o
n
এখানে string-এর প্রতিটি character একবার করে ch variable-এ store হচ্ছে এবং print হচ্ছে।
Using while Loop:
text = "Python"
i = 0
while i < len(text):
print(text[i])
i = i + 1
এখানে len(text) string-এর length দেয়। index ব্যবহার করে প্রতিটি character access করা হয়েছে।
Count Vowels in a String:
text = input("একটি string লিখুন: ")
vowels = "aeiouAEIOU"
count = 0
for ch in text:
if ch in vowels:
count = count + 1
print("Vowel সংখ্যা:", count)
এই program-এ user-এর দেওয়া string-এর প্রতিটি character check করা হচ্ছে। character যদি vowel হয়, তাহলে count 1 করে বাড়ছে।
৯. Built-in Function: len()
len() Python-এর built-in function। এটি string-এর total character সংখ্যা return করে।
Space-ও character হিসেবে count হয়।
Example:
text = "Hello World"
print(len(text))
Output:
11
এখানে Hello 5 character, একটি space, এবং World 5 character — মোট 11।
১০. String Case Conversion Methods
Python-এ string-এর letter case পরিবর্তন করার জন্য বেশ কিছু built-in method আছে। এগুলো text formatting, name formatting, search normalization ইত্যাদি কাজে ব্যবহৃত হয়।
| Method | কাজ | Example |
|---|---|---|
capitalize() |
String-এর প্রথম character uppercase করে এবং বাকিগুলো lowercase করে | "python".capitalize() |
title() |
প্রতিটি word-এর প্রথম character uppercase করে | "python programming".title() |
lower() |
সব character lowercase করে | "PYTHON".lower() |
upper() |
সব character uppercase করে | "python".upper() |
Example:
text = "python programming LANGUAGE"
print(text.capitalize())
print(text.title())
print(text.lower())
print(text.upper())
Output:
Python programming language
Python Programming Language
python programming language
PYTHON PROGRAMMING LANGUAGE
১১. count(), find() এবং index()
কোনো substring কতবার আছে, কোথায় আছে — এগুলো জানার জন্য Python-এ count(),
find() এবং index() methods ব্যবহার করা হয়।
count()
count() method একটি substring string-এর মধ্যে কতবার আছে তা return করে।
text = "banana"
print(text.count("a"))
print(text.count("na"))
Output:
3
2
find()
find() method substring-এর প্রথম occurrence-এর index return করে।
যদি substring না পাওয়া যায়, তাহলে -1 return করে।
text = "Python Programming"
print(text.find("Program"))
print(text.find("Java"))
Output:
7
-1
index()
index() method find()-এর মতোই substring-এর index return করে।
কিন্তু substring না পাওয়া গেলে error দেয়।
text = "Python Programming"
print(text.index("Program"))
Output:
7
find() substring না পেলে -1 দেয়,
কিন্তু index() substring না পেলে ValueError দেয়।
১২. startswith() এবং endswith()
কোনো string নির্দিষ্ট substring দিয়ে শুরু বা শেষ হয়েছে কিনা তা check করতে
startswith() এবং endswith() ব্যবহার করা হয়।
Example:
filename = "report.pdf"
print(filename.startswith("report"))
print(filename.endswith(".pdf"))
print(filename.endswith(".docx"))
Output:
True
True
False
File extension checking, URL checking, prefix validation ইত্যাদিতে এই methods খুব useful।
১৩. String Testing Methods
Python string-এর content কোন ধরনের তা test করার জন্য বেশ কিছু methods আছে। এগুলো সাধারণত validation-এর কাজে ব্যবহৃত হয়।
| Method | কাজ | Example Result |
|---|---|---|
isalnum() |
সব character alphabet বা digit হলে True | "Python123".isalnum() → True |
isalpha() |
সব character alphabet হলে True | "Python".isalpha() → True |
isdigit() |
সব character digit হলে True | "12345".isdigit() → True |
islower() |
সব alphabet lowercase হলে True | "python".islower() → True |
isupper() |
সব alphabet uppercase হলে True | "PYTHON".isupper() → True |
isspace() |
সব character space হলে True | " ".isspace() → True |
Example:
text1 = "Python123"
text2 = "Python"
text3 = "12345"
text4 = " "
print(text1.isalnum())
print(text2.isalpha())
print(text3.isdigit())
print(text4.isspace())
Output:
True
True
True
True
Practical Example: Numeric Input Validation
age = input("আপনার age লিখুন: ")
if age.isdigit():
age = int(age)
print("Age:", age)
else:
print("দয়া করে শুধু number লিখুন")
এখানে user-এর input digit কিনা check করা হয়েছে। যদি digit হয়, তাহলে integer-এ convert করা হয়েছে।
১৪. lstrip(), rstrip() এবং strip()
String-এর শুরু বা শেষে থাকা unwanted spaces remove করার জন্য strip() family methods ব্যবহার করা হয়।
Data cleaning, user input processing এবং file data handling-এ এগুলো খুব গুরুত্বপূর্ণ।
| Method | কাজ |
|---|---|
lstrip() |
String-এর left side-এর spaces remove করে |
rstrip() |
String-এর right side-এর spaces remove করে |
strip() |
String-এর both left এবং right side-এর spaces remove করে |
Example:
text = " Python Programming "
print(text.lstrip())
print(text.rstrip())
print(text.strip())
Practical Example:
username = input("Username লিখুন: ")
username = username.strip()
print("Clean username:", username)
এখানে user যদি username-এর আগে বা পরে extra space দেয়, তাহলে strip() সেটি remove করবে।
১৫. replace()
replace() method string-এর একটি অংশ অন্য অংশ দিয়ে replace করার জন্য ব্যবহার করা হয়।
Syntax:
string.replace(old_value, new_value)
Example:
text = "I like Java"
new_text = text.replace("Java", "Python")
print(new_text)
Output:
I like Python
Multiple Replacement:
text = "apple apple apple"
new_text = text.replace("apple", "mango")
print(new_text)
এখানে সব apple replace হয়ে mango হবে।
১৬. split()
split() method একটি string-কে ছোট ছোট parts-এ ভাগ করে list তৈরি করে।
By default এটি space-এর ভিত্তিতে split করে।
Example:
sentence = "Python is easy to learn"
words = sentence.split()
print(words)
Output:
['Python', 'is', 'easy', 'to', 'learn']
Split by Comma:
data = "Rahim,Karim,Salam"
names = data.split(",")
print(names)
Output:
['Rahim', 'Karim', 'Salam']
১৭. join()
join() method list বা sequence-এর elements-গুলোকে একটি string-এ join করে।
এটি split()-এর opposite হিসেবে ভাবা যায়।
Example:
words = ["Python", "is", "powerful"]
sentence = " ".join(words)
print(sentence)
Output:
Python is powerful
Join with Hyphen:
items = ["2026", "05", "26"]
date = "-".join(items)
print(date)
Output:
2026-05-26
join() method শুধু string elements-এর sequence-এর উপর কাজ করে।
List-এর মধ্যে integer থাকলে আগে string-এ convert করতে হবে।
১৮. partition()
partition() method string-কে তিনটি parts-এ ভাগ করে:
separator-এর আগের অংশ, separator নিজে, এবং separator-এর পরের অংশ।
এটি একটি tuple return করে।
Example:
email = "student@example.com"
result = email.partition("@")
print(result)
Output:
('student', '@', 'example.com')
এখানে @ separator হিসেবে কাজ করেছে।
তাই email তিনটি অংশে ভাগ হয়েছে।
Practical Example:
email = "student@example.com"
username, symbol, domain = email.partition("@")
print("Username:", username)
print("Domain:", domain)
১৯. String Methods Summary Table
| Function / Method | কাজ | Example |
|---|---|---|
len() |
String-এর length দেয় | len("Python") → 6 |
capitalize() |
প্রথম character capital করে | "python".capitalize() |
title() |
প্রতিটি word-এর প্রথম letter capital করে | "hello world".title() |
lower() |
সব lowercase করে | "HELLO".lower() |
upper() |
সব uppercase করে | "hello".upper() |
count() |
Substring কতবার আছে count করে | "banana".count("a") |
find() |
Substring-এর index খুঁজে, না পেলে -1 দেয় | "Python".find("t") |
index() |
Substring-এর index খুঁজে, না পেলে error দেয় | "Python".index("t") |
endswith() |
String নির্দিষ্ট text দিয়ে শেষ হয়েছে কিনা check করে | "file.pdf".endswith(".pdf") |
startswith() |
String নির্দিষ্ট text দিয়ে শুরু হয়েছে কিনা check করে | "Python".startswith("Py") |
isalnum() |
সব character alphabet বা digit কিনা check করে | "abc123".isalnum() |
isalpha() |
সব alphabet কিনা check করে | "abc".isalpha() |
isdigit() |
সব digit কিনা check করে | "123".isdigit() |
islower() |
সব lowercase কিনা check করে | "abc".islower() |
isupper() |
সব uppercase কিনা check করে | "ABC".isupper() |
isspace() |
সব space কিনা check করে | " ".isspace() |
lstrip() |
Left side space remove করে | " hi".lstrip() |
rstrip() |
Right side space remove করে | "hi ".rstrip() |
strip() |
Both side space remove করে | " hi ".strip() |
replace() |
String-এর অংশ replace করে | "Java".replace("Java", "Python") |
join() |
Sequence elements join করে string বানায় | " ".join(["I", "am", "learning"]) |
partition() |
String-কে separator অনুযায়ী ৩ অংশে ভাগ করে | "a@b.com".partition("@") |
split() |
String split করে list বানায় | "a b c".split() |
২০. Complete Practice Program
নিচের program-এ string input, length, case conversion, membership, count, replace, split, join এবং traversal — অনেকগুলো concept একসাথে দেখানো হয়েছে।
text = input("একটি sentence লিখুন: ")
print("Original text:", text)
print("Length:", len(text))
print("Capitalized:", text.capitalize())
print("Title case:", text.title())
print("Lowercase:", text.lower())
print("Uppercase:", text.upper())
word = input("কোন word খুঁজতে চান? ")
if word in text:
print(word, "text-এর মধ্যে আছে")
print("প্রথম পাওয়া গেছে index:", text.find(word))
print("মোট occurrence:", text.count(word))
else:
print(word, "text-এর মধ্যে নেই")
clean_text = text.strip()
print("After strip:", clean_text)
new_text = clean_text.replace("Python", "Programming")
print("After replace:", new_text)
words = clean_text.split()
print("Words list:", words)
joined_text = "-".join(words)
print("Joined text:", joined_text)
print("প্রতিটি character:")
for ch in clean_text:
print(ch)
Program Explanation:
- User থেকে একটি sentence input নেওয়া হয়েছে।
len()দিয়ে total length বের করা হয়েছে।capitalize(),title(),lower(),upper()দিয়ে case formatting দেখানো হয়েছে।- Membership operator
inদিয়ে word আছে কিনা check করা হয়েছে। find()এবংcount()দিয়ে word-এর position এবং occurrence count করা হয়েছে।strip()দিয়ে extra spaces remove করা হয়েছে।replace()দিয়ে text replace করা হয়েছে।split()দিয়ে sentence-কে word list-এ convert করা হয়েছে।join()দিয়ে list-এর words আবার string-এ convert করা হয়েছে।- শেষে loop দিয়ে string traverse করা হয়েছে।
২১. Common Mistakes in Python Strings
- Index out of range: string-এর length-এর বাইরে index access করলে error হয়।
- String immutable ভুলে যাওয়া: string-এর কোনো character সরাসরি change করা যায় না।
- Number এবং string concatenate করা: number-কে আগে
str()দিয়ে convert করতে হয়। - find() এবং index() confuse করা:
find()না পেলে -1 দেয়,index()error দেয়। - strip() ব্যবহার না করা: user input-এর আগে/পরে extra space থাকলে comparison ভুল হতে পারে।
- case-sensitive comparison: Python string comparison case-sensitive। তাই প্রয়োজন হলে
lower()বাupper()ব্যবহার করতে হয়।
Case-sensitive Example:
answer = input("Yes অথবা No লিখুন: ")
if answer.lower() == "yes":
print("You selected Yes")
else:
print("You selected something else")
এখানে user YES, Yes, বা yes যেভাবেই লিখুক,
lower() ব্যবহার করার কারণে comparison সহজ হয়েছে।
উপসংহার
Python programming-এ string একটি অত্যন্ত গুরুত্বপূর্ণ data type। Text data handle করার জন্য string ব্যবহার করা হয় এবং real-world application-এ string-এর ব্যবহার অনেক বেশি। User input, file name, email, password, message, paragraph, search text, report data — সব ক্ষেত্রেই string গুরুত্বপূর্ণ ভূমিকা পালন করে।
String operations যেমন concatenation, repetition, membership এবং slicing আমাদের text data manipulate করতে সাহায্য করে।
আবার loops ব্যবহার করে string traverse করলে প্রতিটি character আলাদাভাবে process করা যায়।
Python-এর built-in string methods যেমন capitalize(), title(), lower(),
upper(), count(), find(), index(), split(),
join(), replace(), strip() ইত্যাদি text processing-কে অনেক সহজ করে তোলে।
একজন beginner programmer-এর জন্য string ভালোভাবে শেখা খুব জরুরি, কারণ string concept ভালোভাবে বুঝলে input validation, data cleaning, text formatting, search operation, file processing এবং real-world software development অনেক সহজ হয়ে যায়।