← Back to Python Course | Chapter 5: Strings | Lesson 6 of 6

Python में String Operations

String operations वे चीज़ें हैं जो आप text के साथ कर सकते हैं, जैसे दो शब्दों को जोड़ना, किसी एक को दोहराना, या यह जाँचना कि एक शब्द दूसरे के अंदर है या नहीं। यह word blocks के साथ खेलने जैसा है।
Syntax
python
string1 + string2
string * n
substring in string
len(string)

String Concatenation

+ operator strings को end to end जोड़कर एक नई string बनाता है; क्योंकि Python में strings immutable हैं, किसी loop में concatenate करना बार-बार नए objects बनाता है, यही वजह है कि बड़ी मात्रा में text के लिए कई टुकड़ों को ''.join(list) से जोड़ना ज़्यादा efficient idiom है।

उदाहरण: String Concatenation

python
first = "Hello"
second = "World"
print(first + " " + second)  # + joins the strings end to end into a new string

String Repetition

किसी string को * से किसी integer के साथ multiply करना उसे उतनी ही बार repeat करता है, जो separators, padding, या '-' * 40 जैसे ASCII-art borders बनाने का एक जल्दी तरीका है, बिना कोई loop लिखे।

उदाहरण: String Repetition

python
print("-" * 10)

Membership Operators

in और 'not in' substring की मौजूदगी जाँचते हैं और एक boolean return करते हैं, जिससे ये ज़्यादा costly parsing करने से पहले membership test करने का सबसे simple तरीका बन जाते हैं -- उदाहरण के लिए किसी address को आगे validate करने की कोशिश करने से पहले '@' in email जाँचना।

उदाहरण: Membership Operators

python
email = "[email protected]"
print("@" in email)

Strings की Comparison

< और > जैसे comparison operators strings की तुलना character by character, Unicode code points का इस्तेमाल करके करते हैं, तो Apple < apple True है क्योंकि uppercase letters lowercase वालों से पहले sort होते हैं -- यह user input को पहले case normalize किए बिना sort करते समय bugs का एक आम कारण है।

उदाहरण: Comparing Strings

python
print("Apple" < "apple")

Strings पर Iterate करना

for loop से किसी string पर loop करना क्रम से एक बार में एक character visit करता है, जो vowels गिनने या range(len(s)) के ज़रिए index-based access के बिना character-frequency map बनाने जैसे कामों के लिए उपयोगी है।

उदाहरण: Iterating over Strings

python
for char in "cat":
    print(char)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.