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

String

The document provides a comprehensive overview of various methods available in the String class in Python, demonstrating their usage with a sample string. Each method is illustrated with examples, including functionalities like capitalization, case conversion, substring searching, and whitespace handling. The document serves as a practical guide for understanding and utilizing string methods effectively in Python programming.
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)
9 views7 pages

String

The document provides a comprehensive overview of various methods available in the String class in Python, demonstrating their usage with a sample string. Each method is illustrated with examples, including functionalities like capitalization, case conversion, substring searching, and whitespace handling. The document serves as a practical guide for understanding and utilizing string methods effectively in Python programming.
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/ 7

For Practice in lab sessions :

Methods of the String class :

# Sample string

text = "Hello, Python World!"

# capitalize() - Capitalizes the first character of the string

print("capitalize():", text.capitalize())

# casefold() - Converts the string to casefolded (lowercase) version

print("casefold():", text.casefold())

# center() - Centers the string within the specified width

print("center(30):", text.center(30))

# count() - Counts the occurrences of a substring in the string

print("count('o'):", text.count('o'))

# encode() - Encodes the string using the specified encoding

print("encode('utf-8'):", text.encode('utf-8'))

# endswith() - Checks if the string ends with the specified suffix

print("endswith('World!'):", text.endswith('World!'))
# expandtabs() - Expands tabs in the string to the specified number of
spaces

tabbed_text = "Hello\tWorld"

print("expandtabs(4):", tabbed_text.expandtabs(4))

# find() - Finds the first occurrence of a substring

print("find('Python'):", text.find('Python'))

# format() - Formats the string using placeholders

formatted_text = "Hello, {}!"

print("format('Alice'):", formatted_text.format('Alice'))

# index() - Finds the first occurrence of a substring and raises an error if


not found

try:

print("index('Python'):", text.index('Python'))

except ValueError as e:

print("index('Python'):", e)

# isalnum() - Checks if all characters in the string are alphanumeric

alnum_text = "Hello123"

print("isalnum():", alnum_text.isalnum())
# isalpha() - Checks if all characters in the string are alphabetic

alpha_text = "Hello"

print("isalpha():", alpha_text.isalpha())

# isascii() - Checks if all characters in the string are ASCII

ascii_text = "Hello"

print("isascii():", ascii_text.isascii())

# isdigit() - Checks if all characters in the string are digits

digit_text = "12345"

print("isdigit():", digit_text.isdigit())

# islower() - Checks if all characters in the string are lowercase

lower_text = "hello"

print("islower():", lower_text.islower())

# isnumeric() - Checks if all characters in the string are numeric

numeric_text = "12345"

print("isnumeric():", numeric_text.isnumeric())

# isprintable() - Checks if all characters in the string are printable

printable_text = "Hello, World!"

print("isprintable():", printable_text.isprintable())
# isspace() - Checks if all characters in the string are whitespace

space_text = " "

print("isspace():", space_text.isspace())

# istitle() - Checks if the string is titled (each word starts with an


uppercase letter)

title_text = "Hello World"

print("istitle():", title_text.istitle())

# isupper() - Checks if all characters in the string are uppercase

upper_text = "HELLO"

print("isupper():", upper_text.isupper())

# join() - Joins elements of an iterable with the string as a separator

iterable = ["Hello", "Python", "World"]

print("join(iterable):", " ".join(iterable))

# ljust() - Left-justifies the string within the specified width

print("ljust(30):", text.ljust(30))

# lower() - Converts all characters to lowercase

print("lower():", text.lower())
# lstrip() - Removes leading whitespace characters

whitespace_text = " Hello"

print("lstrip():", whitespace_text.lstrip())

# maketrans() - Creates a translation table for str.translate()

translation_table = str.maketrans("H", "J")

print("maketrans('H', 'J'):", translation_table)

# translate() - Translates the string using the specified translation table

print("translate(translation_table):", text.translate(translation_table))

# partition() - Splits the string into a tuple at the first occurrence of the
separator

print("partition('Python'):", text.partition('Python'))

# replace() - Replaces all occurrences of a substring with another


substring

print("replace('Python', 'Java'):", text.replace('Python', 'Java'))

# rfind() - Finds the last occurrence of a substring

print("rfind('o'):", text.rfind('o'))
# rindex() - Finds the last occurrence of a substring and raises an error if
not found

try:

print("rindex('o'):", text.rindex('o'))

except ValueError as e:

print("rindex('o'):", e)

# rjust() - Right-justifies the string within the specified width

print("rjust(30):", text.rjust(30))

# rsplit() - Splits the string from the right at the specified separator

print("rsplit(' '):", text.rsplit(' '))

# rstrip() - Removes trailing whitespace characters

whitespace_text = "Hello "

print("rstrip():", whitespace_text.rstrip())

# split() - Splits the string at the specified separator

print("split(' '):", text.split(' '))

# splitlines() - Splits the string at line breaks

multiline_text = "Hello\nWorld"

print("splitlines():", multiline_text.splitlines())
# startswith() - Checks if the string starts with the specified prefix

print("startswith('Hello'):", text.startswith('Hello'))

# strip() - Removes leading and trailing whitespace characters

whitespace_text = " Hello "

print("strip():", whitespace_text.strip())

# swapcase() - Swaps case of all characters in the string

print("swapcase():", text.swapcase())

# title() - Converts the string to title case

print("title():", text.title())

# upper() - Converts all characters to uppercase

print("upper():", text.upper())

# zfill() - Pads the string with zeros on the left until the specified width is
reached

numeric_text = "42"

print("zfill(5):", numeric_text.zfill(5))

You might also like