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

String Methods

String Methods in Python

Uploaded by

lazy.genius.358
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)
1 views12 pages

String Methods

String Methods in Python

Uploaded by

lazy.genius.358
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

String Methods

Introduction
● Strings are sequences of characters.
● Python string methods is a collection of in-built Python functions that operates
on strings.
● Python provides a rich set of built-in string methods to manipulate and work
with text data.
● These methods are accessed using dot notation on a string object (e.g.,
my_string.upper())
● Since strings in Python are immutable these methods return new strings.
● When we perform operations that seem to modify a string, such as
concatenation or using methods like replace() or upper(), Python actually
creates a new string object in a different memory location with the desired
changes, and the variable is then reassigned to this new object.
Example
s = "Hello"

print(id(s))

# prints a memory address

# Creates a *new* string object

s = s + " World"

print(id(s)) # prints a different memory address


Case Modification
● lower():
○ Converts all characters in the string to lowercase.
● upper():
○ Converts all characters in the string to uppercase.
● capitalize():
○ Capitalizes only the first character of the entire string.
● title():
○ Capitalizes the first character of each word in the string.
● swapcase():
○ Swaps the case of all characters (uppercase becomes lowercase and vice versa).
Searching and Finding
● find(value):
○ Searches for a specified value and returns the lowest index where it is found.
○ Returns -1 if the value is not found.
● index(value):
○ Similar to find(), but raises a ValueError exception if the value is not found.
● count(value):
○ Returns the number of times a specified value occurs in the string.
● startswith(value):
○ Returns True if the string starts with the specified value, otherwise False.
● endswith(value):
○ Returns True if the string ends with the specified value, otherwise False.
Modification and Formatting
● strip():
○ Removes any leading and trailing whitespace or specified characters from the string.
● replace(old, new):
○ Replaces all occurrences of a specified "old" substring with a "new" substring.
● split(separator):
○ Splits the string into a list of substrings based on a specified separator.
○ If no separator is provided, it splits on all whitespace characters.
● join(iterable):
○ The opposite of split(). It joins the elements of an iterable (e.g., a list) into a single string using
the original string as the delimiter.
● format() / f-strings:
○ Used for powerful and dynamic string formatting, allowing variables to be inserted into a string
template.
Boolean Checks (Returns True or False)

● isalnum():
○ Returns True if all characters in the string are alphanumeric (A-Z, a-z, 0-9).
● isalpha():
○ Returns True if all characters in the string are in the alphabet.
● isdigit():
○ Returns True if all characters in the string are digits.
● isspace():
○ Returns True if the string contains only whitespace characters.
● NOTE:
○ Learn more about string methods here
● Task 1: Palindrome Checker for Sentences
○ Takes a sentence as input.
○ Removes spaces and punctuation.
○ Converts to lowercase.
○ Returns True if the sentence is a palindrome, else False.
● Task 2: Capitalize Proper Nouns
○ Takes a sentence and a list of proper nouns.
○ Capitalizes only the proper nouns in the sentence.
○ Returns the updated sentence.

You might also like