Python Strings Made Easy: A Beginner's Tutorial (Hindi + English)
๐ The Ultimate Guide to Python Strings ๐
A Beginner-Friendly Tutorial with Simple Code Examples
๐ค What is a String? (String kya hai?)
A string is a sequence of characters. In simple words, it's how you store and manage text in Python.
Ek string characters (letters, numbers, ya symbols) ka ek sequence hota hai. Aasan shabdon mein, yeh Python mein text store karne ke kaam aata hai.
# You can use single, double, or triple quotes
my_name = 'Rohan'
my_city = "Pilibhit"
message = """This is a multi-line string."""
print(my_name)
print(my_city)
print(message)
Rohan Pilibhit This is a multi-line string.
✂️ Indexing & Slicing (Characters ko Access karna)
Indexing helps you get a single character. Slicing helps you get a part of the string (a substring).
Indexing se aap ek character nikalte hain. Slicing se aap string ka ek hissa nikalte hain.
word = 'PYTHON'
# Indexing (Index 0 is the first character)
print(word[0])
# Slicing (Get characters from index 1 up to 3)
print(word[1:4])
P YTH
๐ ️ Most Useful String Methods
Here are the most common and useful methods you'll use every day.
1. len(), lower() & upper()
len() counts characters. .lower() converts to lowercase. .upper() converts to uppercase.
print(len('hello'))
print('INDIA'.lower())
print('india'.upper())
5 india INDIA
2. find() & replace()
.find() searches for text. .replace() finds and replaces text.
print('this is a sentence'.find('is'))
print('I like cats.'.replace('cats', 'dogs'))
2 I like dogs.
3. split() & join()
.split() breaks a string into a list. .join() combines a list into a string.
# split()
print('Work is Worship'.split())
# join()
word_list = ['Work', 'is', 'Worship']
print(' '.join(word_list))
['Work', 'is', 'Worship'] Work is Worship
4. strip(), lstrip() & rstrip()
These methods remove extra whitespace from a string's beginning and/or end.
text = ' hello world '
# strip() removes from both sides
print(f"strip: '{text.strip()}'")
# lstrip() removes from left side
print(f"lstrip: '{text.lstrip()}'")
# rstrip() removes from right side
print(f"rstrip: '{text.rstrip()}'")
strip: 'hello world' lstrip: 'hello world ' rstrip: ' hello world'
Happy Coding! Keep learning and building amazing things with Python.
Comments
Post a Comment