str ক্লাসটি স্ট্রিং নিয়ে কাজ করার ও স্ট্রিং গঠনের জন্য অনেক কাজের মেথড দেয়।
এগুলোর মধ্যে আছে খোঁজা, পরিষ্কার করা, ভাগ করা, রূপান্তর করা, অনুবাদ করা এবং আরও অনেক কৌশল।
স্ট্রিং হলো Unicode কোড পয়েন্টের সিকোয়েন্স। বাম দিক থেকে 0-based index নম্বর দিয়ে, বা ডান দিক থেকে -1-based index নম্বর দিয়ে আলাদা আলাদা "ক্যারেক্টার" বা কোড পয়েন্ট (১ দৈর্ঘ্যের স্ট্রিং) উল্লেখ করা যায়।
স্ট্রিং সব কমন সিকোয়েন্স অপারেশন ইমপ্লিমেন্ট করে।
for item in <str> বা for index, item in enumerate(<str>) সিনট্যাক্স ব্যবহার করে এগুলোর মধ্য দিয়ে ইটারেশন করা যায়।
আবার <str> + <other str> বা <str>.join(<iterable>) দিয়েও এগুলো কনক্যাটেনেট করা যায়।
স্ট্রিং ইমিউটেবল, অর্থাৎ মেমোরিতে একটি str অবজেক্টের মান বদলানো যায় না।
যে ফাংশন বা মেথড একটি str নিয়ে কাজ করে (যেমন এখানে আমরা যেগুলো শিখছি), সেগুলো আসল str পরিবর্তন না করে ওই str অবজেক্টের একটি নতুন instance রিটার্ন করে।
নিচে Python স্ট্রিং মেথডের একটি ছোট নির্বাচন দেওয়া হলো। সম্পূর্ণ তালিকার জন্য Python ডকুমেন্টেশনে str ক্লাস দেখুন।
<str>.title() একটি স্ট্রিং পার্স করে এবং তাতে পাওয়া প্রতিটি "শব্দের" প্রথম "ক্যারেক্টার" বড় হাতের করে।
Python-এ এটি অনেকটাই নির্ভর করে ব্যবহৃত ভাষা কোডেক এবং ওই নির্দিষ্ট ভাষা কীভাবে শব্দ ও ক্যারেক্টার প্রকাশ করে তার উপর।
কোনো ভাষা বা ক্যারেক্টার সেটের জন্য লোকেল নিয়মও থাকতে পারে।
man_in_hat_th = 'ผู้ชายใส่หมวก'
man_in_hat_ru = 'мужчина в шляпе'
man_in_hat_ko = '모자를 쓴 남자'
man_in_hat_en = 'the man in the hat.'
>>> man_in_hat_th.title()
'ผู้ชายใส่หมวก'
>>> man_in_hat_ru.title()
'Мужчина В Шляпе'
>>> man_in_hat_ko.title()
'모자를 쓴 남자'
>> man_in_hat_en.title()
'The Man In The Hat.'
<str>.endswith(<suffix>) রিটার্ন করে True যদি স্ট্রিংটি <suffix> দিয়ে শেষ হয়, নাহলে False।
>>> 'My heart breaks. 💔'.endswith('💔')
True
>>> 'cheerfulness'.endswith('ness')
True
# Punctuation is part of the string, so needs to be included in any endswith match.
>>> 'Do you want to 💃?'.endswith('💃')
False
>> 'The quick brown fox jumped over the lazy dog.'.endswith('dog')
False
<str>.strip(<chars>) str-এর একটি কপি রিটার্ন করে, যেখানে শুরু ও শেষের <chars> মুছে ফেলা হয়।
<chars>-এ নির্দিষ্ট করা কোড পয়েন্টগুলো কোনো প্রিফিক্স বা সাফিক্স নয়। স্ট্রিংয়ের দুই প্রান্ত থেকেই কোড পয়েন্টগুলোর সব কম্বিনেশন মুছে ফেলা হবে।
<chars>-এর জন্য কিছু নির্দিষ্ট না করা হলে, হোয়াইটস্পেস কোড পয়েন্টের সব কম্বিনেশন মুছে ফেলা হবে।
# This will remove "https://", because it can be formed from "/stph:".
>>> 'https://unicode.org/emoji/'.strip('/stph:')
'unicode.org/emoji'
# Removal of all whitespace from both ends of the str.
>>> ' 🐪🐪🐪🌟🐪🐪🐪 '.strip()
'🐪🐪🐪🌟🐪🐪🐪'
>>> justification = 'оправдание'
>>> justification.strip('еина')
'оправд'
# Prefix and suffix in one step.
>>> 'unaddressed'.strip('dnue')
'address'
>>> ' unaddressed '.strip('dnue ')
'address'
<str>.replace(<substring>, <replacement substring>) স্ট্রিংয়ের একটি কপি রিটার্ন করে, যেখানে <substring>-এর প্রতিটি উপস্থিতি <replacement substring> দিয়ে বদলে দেওয়া হয়।
নিচে ব্যবহৃত উদ্ধৃতিটি Lewis Carroll-এর The Hunting of the Snark থেকে নেওয়া।
# The Hunting of the Snark, by Lewis Carroll
>>> quote = '''
"Just the place for a Snark!" the Bellman cried,
As he landed his crew with care;
Supporting each man on the top of the tide
By a finger entwined in his hair.
"Just the place for a Snark! I have said it twice:
That alone should encourage the crew.
Just the place for a Snark! I have said it thrice:
What I tell you three times is true."
'''
>>> quote.replace('Snark', '🐲')
...
'\n"Just the place for a 🐲!" the Bellman cried,\n As he landed his crew with care;\nSupporting each man on the top of the tide\n By a finger entwined in his hair.\n\n"Just the place for a 🐲! I have said it twice:\n That alone should encourage the crew.\nJust the place for a 🐲! I have said it thrice:\n What I tell you three times is true."\n'
>>> 'bookkeeper'.replace('kk', 'k k')
'book keeper'
এই অনুশীলনীতে আপনি আপনার ছোট বোনকে স্কুলের জন্য তার পেপার এডিট করতে সাহায্য করছেন। শিক্ষক খুঁজছেন সঠিক যতিচিহ্ন, ব্যাকরণ আর চমৎকার শব্দচয়ন।
স্ট্রিংগুলো পরিষ্কার ও পরিবর্তন করার জন্য আপনার সামনে চারটি কাজ রয়েছে।
যেকোনো ভালো পেপারের জন্যই সঠিকভাবে ফরম্যাট করা একটি শিরোনাম প্রয়োজন।
capitalize_title(<title>) ফাংশনটি ডিফাইন করুন, যা প্যারামিটার হিসেবে একটি শিরোনাম str নেয় এবং প্রতিটি শব্দের প্রথম অক্ষর বড় হাতের করে।
এই ফাংশনটি টাইটেল কেসে একটি str রিটার্ন করবে।
>>> capitalize_title("my hobbies")
"My Hobbies"
আপনি নিশ্চিত করতে চান যে পেপারের যতিচিহ্ন নিখুঁত।
check_sentence_ending() ফাংশনটি ডিফাইন করুন, যা প্যারামিটার হিসেবে sentence নেয়। এই ফাংশনটি একটি bool রিটার্ন করবে।
>>> check_sentence_ending("I like to hike, bake, and read.")
True
পেপারটিকে পেশাদার দেখাতে অপ্রয়োজনীয় স্পেসিং সরিয়ে ফেলা দরকার।
clean_up_spacing() ফাংশনটি ডিফাইন করুন, যা প্যারামিটার হিসেবে sentence নেয়।
ফাংশনটি বাক্যের শুরুতে ও শেষে থাকা বাড়তি হোয়াইটস্পেস সরিয়ে ফেলবে এবং হালনাগাদ করা নতুন একটি str বাক্য রিটার্ন করবে।
>>> clean_up_spacing(" I like to go on hikes with my dog. ")
"I like to go on hikes with my dog."
পেপারটিকে আরও ভালো করতে আপনি কিছু বিশেষণকে তার প্রতিশব্দ দিয়ে প্রতিস্থাপন করতে পারেন।
replace_word_choice() ফাংশনটি লিখুন, যা প্যারামিটার হিসেবে sentence, old_word এবং new_word নেয়।
এই ফাংশনটি old_word-এর সব ইনস্ট্যান্স new_word দিয়ে প্রতিস্থাপন করবে এবং হালনাগাদ করা বাক্যসহ একটি নতুন str রিটার্ন করবে।
>>> replace_word_choice("I bake good cakes.", "good", "amazing")
"I bake amazing cakes."
Exercism-এ সাইন আপ করুন, Python ট্র্যাকের 17টি কনসেপ্ট146টি অনুশীলনী আর সত্যিকারের মানুষের মেন্টরিং দিয়ে শিখুন ও দক্ষ হয়ে উঠুন, সম্পূর্ণ বিনামূল্যে।