Strings के साथ काम

डेवलपर्स के लिए Python परिचय

Jasmin Ludolf

Senior Data Science Content Developer

Strings हर जगह हैं!

स्ट्रिंग से लिखा Hello

  • संदेश दिखाना
  • टेक्स्ट इनपुट प्रोसेस करना
  • फ़ाइल नाम
  • आउटपुट फ़ॉर्मैट करना
  • डेटा हैंडल करना
1 Image generated by ChatGPT
डेवलपर्स के लिए Python परिचय

Python में single और double quotes

# This works
ingredient_name = 'San Marzano tomatoes'

# This also works
ingredient_name = "San Marzano tomatoes"
  • ' = apostrophe = single quote
डेवलपर्स के लिए Python परिचय

Double quotes के फायदे

# Single quote string variable containing an apostrophe
ingredient_name = 'Chef's special seasoning'
print(ingredient_name)
SyntaxError: invalid syntax.
# Double quote string variable containing an apostrophe
ingredient_name = "Chef's special seasoning"
print(ingredient_name)
Chef's special seasoning
डेवलपर्स के लिए Python परिचय

वाक्य और पैराग्राफ

recipe_step = "Heat olive oil in a large pan and sauté garlic until fragrant"
डेवलपर्स के लिए Python परिचय

मल्टीलाइन strings

# Create a string variable over multiple lines
recipe_instructions = """1. Bring a large pot of salted water to boil and cook pasta
2. Heat olive oil in a pan and sauté minced garlic until fragrant
3. Add chopped tomatoes and simmer for 10 minutes
4. Toss cooked pasta with tomato sauce and fresh basil leaves
"""
  • """text""": मल्टीलाइन strings
    • पठनीयता बढ़ाएँ
    • डॉक्यूमेंटेशन में उपयोग
    • लंबे टेक्स्ट जैसे निर्देश या एरर संदेश
डेवलपर्स के लिए Python परिचय

Methods

  • Method = ऐसा function जो किसी खास data type के लिए उपलब्ध हो

  • str methods

    • इनपुट को मानकीकृत करना, या टेक्स्ट ट्रांसफॉर्म करना
# Calling a string method
string_variable.method()
डेवलपर्स के लिए Python परिचय

Strings के हिस्से बदलना

  • .replace(text_to_be_replaced, text_to_change_it_to)
welcome_message = "Welcome to the recipe scaler, George"

welcome_message = welcome_message.replace("George", "John")

print(welcome_message)
Welcome to the recipe scaler, John
डेवलपर्स के लिए Python परिचय

Case बदलना

  • यूज़र इनपुट जैसे ईमेल को मानकीकृत करें
ingredient_name = "Basil Leaves"

# Convert to lowercase ingredient_name = ingredient_name.lower() print(ingredient_name)
basil leaves
# Change to uppercase
ingredient_name = ingredient_name.upper()
print(ingredient_name)
BASIL LEAVES
डेवलपर्स के लिए Python परिचय

अभ्यास करते हैं!

डेवलपर्स के लिए Python परिचय

Preparing Video For Download...