0% found this document useful (0 votes)
1 views7 pages

String Methods

The document explains string methods in Python, detailing what strings are, their immutability, and various built-in methods for manipulating strings. It covers indexing, slicing, and common operations such as case conversion, searching, and validation. Additionally, it provides examples and a summary table of string concepts and methods.

Uploaded by

snehitha02468
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)
1 views7 pages

String Methods

The document explains string methods in Python, detailing what strings are, their immutability, and various built-in methods for manipulating strings. It covers indexing, slicing, and common operations such as case conversion, searching, and validation. Additionally, it provides examples and a summary table of string concepts and methods.

Uploaded by

snehitha02468
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

STRING METHODS:

🔠 What is a String in Python?


A string is a sequence of characters enclosed in single ('), double ("), or triple quotes (''' or """).
# Examples of string:
name = 'Python'
quote = "Learning is fun"
multiline = '''This is a multiline string.'''
Strings are immutable, meaning once created, you cannot change their content directly.
🧰 What are String Methods?
String methods are built-in functions that can be called on string objects to perform common
operations like changing case, finding substrings, replacing text, and more.

🎯 Where Do We Use String Methods?


1. Data Cleaning: Removing whitespace, converting cases.
[Link] Processing: Extracting information, formatting.
[Link]: Checking if user input is alphabetic, numeric, etc.

⭐ 1. Indexing in Python

Indexing means accessing individual characters or elements of a string/list using their position.

✔ Index always starts from 0 (left side)

s = "python"

Index: 0 1 2 3 4 5

Letter: p y t h o n

✔ Access using []

print(s[0]) # p

print(s[3]) # h

print(s[5]) # n

✔ Negative Indexing (from right side)

s = "python"

Index: -6 -5 -4 -3 -2 -1

Letter: p y t h o n
Examples:

print(s[-1]) # n

print(s[-3]) # h

print(s[-6]) # p

⭐ 2. Slicing in Python

Slicing means extracting a part of the string/list.

✔ Syntax:

string[start:end]

 start → starting index (included)

 end → ending index (excluded)

✔ Example:

s = "python"

print(s[0:3]) # pyt

print(s[2:6]) # thon

✔ Omitting values:

print(s[:4]) # pyth (start from 0)

print(s[3:]) # hon (end till last)

print(s[:]) # python (whole string)

✔ Negative slicing:

s = "python"

print(s[-4:-1]) # tho

print(s[-6:-3]) # pyt
✔ Step value in slicing (jumping)

Syntax:

string[start:end:step]

Examples:

print(s[0:6:2]) # pto

print(s[::-1]) # nohtyp (reverse string)

print(s[::2]) # pto

⭐ 3. Length in Python

To find number of characters/elements → use len() function.

✔ Example:

s = "python"

print(len(s)) #6

Works for:

 strings

 lists

 tuples

 dictionaries (counts keys)

Example:

l = [10, 20, 30, 40]

print(len(l)) # 4

🎯 Summary Table

Concept Meaning Example

Indexing Accessing single elements s[2] → 't'

Negative Indexing From last to first s[-1] → 'n'


Concept Meaning Example

Slicing Extracting part of string s[1:4] → 'yth'

Slicing with step Jumping characters s[0:6:2] → 'pto'

Reverse slicing Reverse string s[::-1]

🔹 String Indexing
s = "python"
print(s[0]) # p
print(s[5]) # n
print(s[-1]) # n
print(s[-2]) # o

🔹 String Slicing
s = "python"
print(s[1:4]) # yth
print(s[:3]) # pyt
print(s[3:]) # hon
print(s[::-1]) # nohtyp

🔹 1. lower() / upper()
Converts the string to lowercase or uppercase.
s = "Hello"
print([Link]()) # hello
print([Link]()) # HELLO
🔹 2. title() / capitalize()
s = "hello world"
print([Link]()) # Hello World
print([Link]()) # Hello world
🔹 3. strip() / lstrip() / rstrip()
Removes spaces.
s = " python "
print([Link]()) # python
print([Link]()) # python
print([Link]()) # python
🔹 4. replace(old, new)
s = "I love python"
print([Link]("python", "coding"))
🔹 5. split()
Converts string → list
s = "apple,banana,orange"
print([Link](","))
🔹 6. join()
List → String
words = ["hello", "world"]
print(" ".join(words)) # hello world
🔹 7. startswith() / endswith()
s = "python"
print([Link]("py")) # True
print([Link]("on")) # True
🔹 8. find() / index()
Search substring.
s = "programming"
print([Link]("g")) # 3
print([Link]("g")) # 3
9. count()
s = "banana"
print([Link]("a")) # 3
🔹 10. isnumeric(), isalpha(), isalnum(), isspace()
"123".isnumeric() # True
"abc".isalpha() # True
"abc123".isalnum() # True
" ".isspace() # True
🔹 11. len()
(Not a method, but commonly used)
len("python") # 6

Using f-Strings (Best & Most Used) ✅


👉 Introduced in Python 3.6
👉 Fast, readable, interview favorite
name = "Jaya"
age = 20
print(f"My name is {name} and my age is {age}")

problem solving:
✔️1. Reverse a string
s = "python"
print(s[::-1])
✔️2. Count vowels
s = "education"
count = 0
for ch in s:
if ch in "aeiouAEIOU":
count += 1
print(count)
✔️3. Check palindrome
s = "madam"
print(s == s[::-1]) # True
✔️4. Remove spaces
s = "a b c d"
print([Link](" ",""))
✔️5. Count words
s = "python is easy"
print(len([Link]()))
Total String Methods: 47
Case Conversion
lower(), upper(), title(), capitalize(), swapcase(), casefold()
Search / Find
find(), rfind(), index(), rindex()
Check / Test
isalnum(), isalpha(), isdigit(), isnumeric(), isdecimal(), islower(), isupper(), istitle(), isspace(),
isidentifier(), isprintable(), isascii()
Replace / Trim
replace(), strip(), lstrip(), rstrip()
Split & Join
split(), rsplit(), splitlines(), join()
Alignment / Padding
center(), ljust(), rjust(), zfill()
Count & Formatting
count(), format(), format_map(), encode()
Start / End Check
startswith(), endswith()
Translation
maketrans(), translate()
New Methods
removeprefix(), removesuffix()
Others
partition(), rpartition(), expandtabs()

You might also like