String में Capital Letter से Start होने वाले Words Find करना
ऊपर दिए गए String में चार Words हैं जो Uppercase Character से Start होते हैं: This, Delhi, City, India.
इस Purpose के लिए दो String Functions उपयोग करने चाहिए। यह मानते हुए कि String में Words Single Space Character से Separated हैं, split() Function Words की List देता है।
दूसरे, Check करने के लिए कि प्रत्येक Word का First Character Uppercase है या नहीं, isupper() Function का उपयोग करें।
Capital Letters से Start होने वाले Words List करने वाला Code:
s1 = "This is not true that Delhi is the hottest or coldest City in India"
for word in s1.split():
if word[0].isupper():
print(word)
Output:
This Delhi City India
अन्य उपयोगी Variations:
1. String Starts with Capital Letter (Entire String):
s = "Hello world" print(s[0].isupper()) # True
2. Title Case Check (प्रत्येक Word Capitalized):
s = "Hello World" print(s.istitle()) # True s = "Hello world" print(s.istitle()) # False
3. सभी Capitalized Words Extract करना:
import re s = "This is Delhi City in India" capitalized = re.findall(r'\b[A-Z][a-z]*\b', s) print(capitalized) # ['This', 'Delhi', 'City', 'India']
4. First Character Check (Safe):
def starts_with_capital(s):
return bool(s) and s[0].isupper()
print(starts_with_capital("Hello")) # True
print(starts_with_capital("")) # False
print(starts_with_capital(" hello")) # False