Lecture 8 (Strings in depth in python)
Lecture 8 (Strings in depth in python)
🔤 What is a String?
A string is a sequence of characters enclosed in single (' '), double (" "), or
triple (''' or """) quotes.
Strings are immutable, meaning once created, their content cannot be changed.
s1 = 'Hello'
s2 = "World"
s3 = """Multiline
String"""
s = "Python"
print(s[0]) # 'P'
print(s[-1]) # 'n'
print(s[1:4]) # 'yth'
print(s[:3]) # 'Pyt'
print(s[3:]) # 'hon'
print(s[::-1]) # 'nohtyP' (reversed string)
Syntax: string[start:stop:step]
String ➜ List
List ➜ String
bool("hello") # True
bool("") # False
s = "madam"
print(s == s[::-1]) # True
vowels = "aeiou"
s = "education"
count = sum(1 for ch in s if ch.lower() in vowels) # 5
s = "banana"
print("".join(dict.fromkeys(s))) # 'ban'
🔹 Check Anagram
s1 = "listen"
s2 = "silent"
print(sorted(s1) == sorted(s2)) # True
s = "hello world"
" ".join(word[::-1] for word in s.split()) # 'olleh dlrow'
Escape Meaning
\n New Line
\t Tab Space
\' Single Quote
\" Double Quote
\\ Backslash