0% found this document useful (0 votes)
3 views

Lecture 8 (Strings in depth in python)

This document provides an overview of strings in Python, including their definition, immutability, and various string operations such as slicing, indexing, and common methods. It also covers string and list conversions, boolean nature of strings, and answers common questions related to string manipulation. Additionally, it includes information on escape characters used in strings.

Uploaded by

loginoptions6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 8 (Strings in depth in python)

This document provides an overview of strings in Python, including their definition, immutability, and various string operations such as slicing, indexing, and common methods. It also covers string and list conversions, boolean nature of strings, and answers common questions related to string manipulation. Additionally, it includes information on escape characters used in strings.

Uploaded by

loginoptions6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

🧵 Strings in Python (Lecture 8)

YouTube Reference: Watch Lecture

🔤 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"""

✂️String Slicing & Indexing

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]

🧰 Most Useful String Methods

s = " Hello, Python! "


s.lower() # ' hello, python! '
s.upper() # ' HELLO, PYTHON! '
s.strip() # 'Hello, Python!' (removes leading & trailing spaces)
s.lstrip() # removes only leading spaces
s.rstrip() # removes only trailing spaces
s.replace("Python", "World") # ' Hello, World! '
s.split(",") # [' Hello', ' Python! ']
s.find("Python") # 9 (starting index), -1 if not found
s.count("o") #2
len(s) # 17 (includes spaces)

"{} is {}".format("Python", "awesome") # 'Python is awesome'


f"{s} is awesome" # f-strings for formatting

🔁 String and List Conversion

String ➜ List

sentence = "apple banana cherry"


words = sentence.split() # ['apple', 'banana', 'cherry']

List ➜ String

words = ['one', 'two', 'three']


text = " ".join(words) # 'one two three'

Joining without space:

chars = ['H', 'e', 'y']


"".join(chars) # 'Hey'

✅ Boolean Nature of Strings

 Any non-empty string is True


 Empty string ("") is False

bool("hello") # True
bool("") # False

❓ Common Questions on Strings

🔹 Check for Palindrome

s = "madam"
print(s == s[::-1]) # True

🔹 Count Vowels in a String

vowels = "aeiou"
s = "education"
count = sum(1 for ch in s if ch.lower() in vowels) # 5

🔹 Remove Duplicates While Preserving Order

s = "banana"
print("".join(dict.fromkeys(s))) # 'ban'

🔹 Check Anagram

s1 = "listen"
s2 = "silent"
print(sorted(s1) == sorted(s2)) # True

🔹 Reverse Each Word in a String

s = "hello world"
" ".join(word[::-1] for word in s.split()) # 'olleh dlrow'

🔹 Reverse Word Order


s = "life is beautiful"
" ".join(s.split()[::-1]) # 'beautiful is life'

💬 Escape Characters in Strings

Escape Meaning
\n New Line
\t Tab Space
\' Single Quote
\" Double Quote
\\ Backslash

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy