0% found this document useful (0 votes)
3 views3 pages

Unit 3 2

The document outlines various string object methods in Python, including count(), endswith(), startswith(), replace(), strip(), split(), lower(), and upper(), along with their purposes and examples. It also covers regular expression functions like findall(), search(), split(), and sub(), as well as vectorized string operations using pandas such as cat(), count(), get(), isalnum(), isalpha(), islower(), isupper(), and len(). Each method is accompanied by code snippets demonstrating its usage.

Uploaded by

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

Unit 3 2

The document outlines various string object methods in Python, including count(), endswith(), startswith(), replace(), strip(), split(), lower(), and upper(), along with their purposes and examples. It also covers regular expression functions like findall(), search(), split(), and sub(), as well as vectorized string operations using pandas such as cat(), count(), get(), isalnum(), isalpha(), islower(), isupper(), and len(). Each method is accompanied by code snippets demonstrating its usage.

Uploaded by

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

String Object Methods :-

1️⃣ count()

Purpose: Count non-overlapping occurrences of a substring

text = "banana"

print(text.count("a")) # Output: 3

2️⃣ endswith()

Purpose: Check if a string ends with the given suffix

text = "hello.py"

print(text.endswith(".py")) # Output: True

3️⃣ startswith()

Purpose: Check if a string starts with the given prefix

text = "hello.py"

print(text.startswith("hel")) # Output: True

4️⃣ replace()

Purpose: Replace occurrences of a substring with another

text = "I like Java"

print(text.replace("Java", "Python")) # Output: I like Pytho

5️⃣ strip()

Purpose: Remove whitespace (or specified characters) from both ends

text = " hello "

print(text.strip()) # Output: "hello

6️⃣ split()

Purpose: Split string into list using delimiter

text = "a,b,c"

print(text.split(",")) # Output: ['a', 'b', 'c']

7️⃣ lower()

Purpose: Convert all characters to lowercase

text = "HELLO"

print(text.lower()) # Output: "hello"

8️⃣ upper()

Purpose: Convert all characters to uppercase

text = "hello"

print(text.upper()) # Output: "HELLO"


Regular Expression Functions :-
1️) findall()

Returns a list containing all matches

import re

text = "apple banana apple mango"

print(re.findall("apple", text)) # Output: ['apple', 'apple']

2️) search()

Returns a match object if there is a match anywhere in the string

import re

text = "I love Python"

match = re.search("Python", text)

print(match) # Output: <re.Match object; span=(7, 13), match='Python'>

print(match.group()) # Output: Python

3️) split()

Splits the string at each match and returns a list

import re

text = "apple1banana2mango"

print(re.split("[0-9]", text)) # Output: ['apple', 'banana', 'mango']

4️) sub()

Replaces one or many matches with a string

import re

text = "Hello 123, hi 456"

print(re.sub("[0-9]+", "#", text)) # Output: Hello #, hi #


Vectorized string operations :-
1️) cat() – Concatenate string element-wise with optional delimiter

import pandas as pd

s = pd.Series(['Hello', 'World'])

print(s.str.cat(sep=' ')) # Output: Hello Worl

2️) count() – Count occurrences of pattern

s = pd.Series(['apple', 'banana', 'applepie'])

print(s.str.count('apple')) # Output: [1, 0, 1

3️) get() – Index into each element

s = pd.Series(['cat', 'dog', 'rat'])

print(s.str.get(1)) # Output: ['a', 'o', 'a'

4️) isalnum() – Check if all characters are alphanumeric

s = pd.Series(['abc123', 'abc!', '123'])

print(s.str.isalnum()) # Output: [True, False, True

5️) isalpha() – Check if all characters are alphabets

s = pd.Series(['abc', '123', 'abc123'])

print(s.str.isalpha()) # Output: [True, False, False

6️) islower() – Check if all characters are lowercase

s = pd.Series(['hello', 'Hello', 'HELLO'])

print(s.str.islower()) # Output: [True, False, False]

7️) isupper() – Check if all characters are uppercase

s = pd.Series(['HELLO', 'Hello', 'hello'])

print(s.str.isupper()) # Output: [True, False, False

8️) len() – Compute length of each string

s = pd.Series(['apple', 'banana', ''])

print(s.str.len()) # Output: [5, 6, 0]

You might also like