Strings In Python - Python Notes By Mr. BILRED | TechAmbitionX
Strings in Python — Say Hello to Text
A string in Python is just a bunch of characters inside quotes.
Single quotes or double quotes — both work:
name = "Bilal"
greeting = 'Hello World'
empty = ""
They can contain numbers, symbols, even emojis:
info = "Python 3.11 rocks! 🤘"
How to Use Strings in Python
1. Concatenation (Joining Strings)
first = "Mr. "
last = "BILRED"
full = first + last
print(full) # Mr. BILRED
2. String Length
name = "Bilal"
print(len(name)) # 5
3. Accessing Characters
name = "Bilal"
print(name[0]) # B
print(name[-1]) # l
4. Slicing Strings (obv that means k "kaatna")
print(name[0:3]) # Bil
print(name[:4]) # Bila
print(name[2:]) # lal
5. String Methods
msg = "python is awesome"
print(msg.upper()) # PYTHON IS AWESOME
print(msg.capitalize()) # Python is awesome
print(msg.replace("awesome", "fun")) # python is fun
print("tech" in msg) # False
6. Multiline Strings
story = """This is a
multi-line
string."""
print(story)
7. formatted Strings (Python 3.6+)
name = "Bilal"
age = 20
print(f"My name is {name} and I am {age} years old.")
REMEMBER: Anything inside quotes is a string — even numbers like "1234" are strings.
That’s it for strings. For now...
SSBkb24ndCBrbm93IGhvdyBsb25nIEknbGwgbGl2ZSwgYnV0IG1hbiwgSSB3YW5uYSBtYWtlIHRoZSBtb3N0IG9mIG15IExpZmUuLi4=I just wanted to say that...
By the way, if you wanna learn python, visit sites like w3schools! they can be helpful...
Next Post:

Comments
Post a Comment