Data Science · मराठी आवृत्ती · Python basics
P3.5 in, startswith आणि endswith ने Text कसा तपासायचा?
हे का शिकायचं?
Order cancel झाला का, email gmail चा आहे का, ID कोणत्या city चा आहे — असे प्रश्न text तपासून सुटतात.
हे काय आहे?
"x" in s शब्द आत आहे का सांगतो. s.startswith() सुरुवात आणि s.endswith() शेवट तपासतो. उत्तर True किंवा False येतं.
कसं करायचं?
Step 1. Case एकसारखा करा (lower()).
Step 2. in, startswith किंवा endswith वापरा.
Step 3. True/False वरून निर्णय घ्या.
उदाहरणं: copy करा आणि चालवा
उदाहरण A: शब्द आत आहे का?
हा code चालवा:
ticket = "Customer wants a refund for order 778"
print("refund" in ticket)
print("cancel" in ticket)
तुम्हाला दिसणारा output:
True
False
याचा अर्थ: in ने True/False मिळालं. आधी lower() केलं म्हणून case चा फरक पडला नाही.
उदाहरण B: सुरुवात आणि शेवट तपासा
हा code चालवा:
code = "BLK-PUN-1001"
file = "sales_march.csv"
print(code.startswith("BLK"))
print(file.endswith(".csv"))
तुम्हाला दिसणारा output:
True
True
याचा अर्थ: startswith ID ची city आणि endswith email चं domain तपासतो.