একটি str হলো Unicode কোড পয়েন্টের একটি ইমিউটেবল সিকোয়েন্স।
এর মধ্যে থাকতে পারে অক্ষর, ডায়াক্রিটিক্যাল চিহ্ন, পজিশনিং ক্যারেক্টার, সংখ্যা, কারেন্সি সিম্বল, ইমোজি, যতিচিহ্ন, স্পেস ও লাইন ব্রেক, আরও নানা কিছু।
স্ট্রিং সব কমন সিকোয়েন্স অপারেশন ইমপ্লিমেন্ট করে এবং for item in <str> বা for index, item in enumerate(<str>) সিনট্যাক্স ব্যবহার করে ইটারেট করা যায়।
আলাদা আলাদা কোড পয়েন্ট (১ দৈর্ঘ্যের স্ট্রিং) বাম দিক থেকে 0-based index নম্বর দিয়ে, অথবা ডান দিক থেকে -1-based index নম্বর দিয়ে রেফার করা যায়।
<str> + <other str> বা <str>.join(<iterable>) দিয়ে স্ট্রিং কনক্যাটিনেট করা যায় এবং <str>.split(<separator>) দিয়ে ভাগ করা যায়।
এগুলো ফরম্যাটিং ও অ্যাসেম্বলির আরও নানা অপশনও দেয়।
স্ট্রিং নিয়ে আরও কাজ করার জন্য Python খোঁজা, পরিষ্কার করা, রূপান্তর করা, অনুবাদ করা এবং আরও অনেক কাজের জন্য সমৃদ্ধ এক সেট স্ট্রিং মেথড দেয়।
বেশি ব্যবহৃত কিছু str মেথডের মধ্যে আছে:
<str>.startswith(<substr>) ও <str>.endswith(<substr>) দিয়ে প্রিফিক্স/সাফিক্স আছে কিনা পরীক্ষা করা<str>.title(), <str>.upper()/<str>.lower() আর <str>.swapcase()-এর মতো মেথড দিয়ে স্ট্রিংয়ের কেস বদলানো<str>.strip(<chars>), <str>.lstrip(<chars>) বা <str>.rstrip(<chars>) ব্যবহার করে স্ট্রিং থেকে শুরু বা শেষের ক্যারেক্টার মুছে ফেলা<str>.replace(<old>, <new>) মেথড দিয়ে সাবস্ট্রিং প্রতিস্থাপন করাstr ইমিউটেবল হওয়ায় মেমোরিতে একটি str অবজেক্টের মান বদলানো যায় না; যে মেথডগুলো স্ট্রিং পরিবর্তন করে বলে মনে হয়, সেগুলো ওই str অবজেক্টেরই একটি নতুন কপি বা ইনস্ট্যান্স রিটার্ন করে।
<str>.endswith(<suffix>) স্ট্রিংটি <suffix> দিয়ে শেষ হলে True রিটার্ন করে, নাহলে 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>.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>.strip(<chars>) একটি str-এর কপি রিটার্ন করে, যেখানে শুরু ও শেষের <chars> মুছে ফেলা হয়।
<chars>-এ উল্লেখ করা কোড পয়েন্টগুলো কোনো প্রিফিক্স বা সাফিক্স নয়; কোড পয়েন্টগুলোর সব কম্বিনেশন স্ট্রিংয়ের দুই প্রান্ত থেকে মুছে ফেলা হবে।
<chars>-এর জন্য কিছু উল্লেখ না করা হলে হোয়াইটস্পেস কোড পয়েন্টগুলোর সব কম্বিনেশন মুছে ফেলা হবে।
শুধু বাম দিক অথবা ডান দিক থেকে মুছতে চাইলে <str>.lstrip(<chars>) ও <str>.rstrip(<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'
🌟Python 3.9-এ সদ্য যোগ করা হয়েছে
Python 3.9 দুটি নতুন স্ট্রিং মেথড নিয়ে এসেছে, যা দিয়ে প্রিফিক্স ও সাফিক্স মোছা অনেক সহজ হয়।
<str>.removeprefix(<substring>) প্রিফিক্স ছাড়া স্ট্রিংটি রিটার্ন করে (string[len(<substring>):])। <substring> না থাকলে মূল স্ট্রিংয়ের একটি কপি রিটার্ন করা হবে।
# removing a prefix
>>> 'TestHook'.removeprefix('Test')
'Hook'
>>> 'bookkeeper'.removeprefix('book')
'keeper'
<str>.removesuffix(<substring>) সাফিক্স ছাড়া স্ট্রিংটি রিটার্ন করে (string[:-len(substring)])। <substring> না থাকলে মূল স্ট্রিংয়ের একটি কপি রিটার্ন করা হবে।
# removing a suffix
>>> 'TestHook'.removesuffix('Hook')
'Test'
>>> 'bookkeeper'.removesuffix('keeper')
'book'
আরও উদাহরণ ও মেথডের জন্য অনানুষ্ঠানিক টিউটোরিয়াল ভালো একটি সূচনা বিন্দু। Python ডকুমেন্টেশনের How to Unicode-এ Unicode, এনকোডিং, বাইট এবং Python-এ স্ট্রিং নিয়ে কাজ করার আরও নানা টেকনিক্যাল দিক নিয়ে দারুণ বিস্তারিত আছে।
Python re মডিউলের মাধ্যমে রেগুলার এক্সপ্রেশনও সাপোর্ট করে, যা ভবিষ্যতে একটি কনসেপ্টে আলোচনা করা হবে।