0% found this document useful (0 votes)
6 views4 pages

Strings Methods

The document provides an overview of various string methods in Python, categorized into frequently used, moderately used, less commonly used, and rarely used methods. Each method is described with its functionality and an example demonstrating its use. This serves as a comprehensive reference for string manipulation techniques in Python.

Uploaded by

THEJAS KUMAR
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)
6 views4 pages

Strings Methods

The document provides an overview of various string methods in Python, categorized into frequently used, moderately used, less commonly used, and rarely used methods. Each method is described with its functionality and an example demonstrating its use. This serves as a comprehensive reference for string manipulation techniques in Python.

Uploaded by

THEJAS KUMAR
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/ 4

1.

Frequently Used String Methods

1. str.lower()

o Converts all characters in a string to lowercase.

o Example: "HELLO".lower() → "hello"

2. str.upper()

o Converts all characters to uppercase.

o Example: "hello".upper() → "HELLO"

3. str.strip()

o Removes leading and trailing whitespace (or specified characters).

o Example: " hello ".strip() → "hello"

4. str.split()

o Splits a string into a list using a specified delimiter (default is whitespace).

o Example: "a,b,c".split(',') → ['a', 'b', 'c']

5. str.join()

o Joins elements of an iterable (e.g., list) into a string, separated by the string calling
the method.

o Example: ",".join(['a', 'b', 'c']) → "a,b,c"

6. str.replace()

o Replaces occurrences of a substring with another substring.

o Example: "hello world".replace("world", "Python") → "hello Python"

7. str.find()

o Returns the index of the first occurrence of a substring or -1 if not found.

o Example: "hello".find('e') → 1

8. str.startswith()

o Checks if a string starts with the specified prefix.

o Example: "hello".startswith('he') → True

9. str.endswith()

o Checks if a string ends with the specified suffix.

o Example: "hello".endswith('lo') → True

10. str.format()

o Performs string formatting.

o Example: "Hello, {}".format("world") → "Hello, world"


2. Moderately Used String Methods

1. str.title()

o Capitalizes the first letter of each word.

o Example: "hello world".title() → "Hello World"

2. str.capitalize()

o Capitalizes the first letter of the string.

o Example: "hello".capitalize() → "Hello"

3. str.count()

o Counts the occurrences of a substring.

o Example: "banana".count('a') → 3

4. str.isdigit()

o Checks if the string contains only digits.

o Example: "123".isdigit() → True

5. str.isalpha()

o Checks if the string contains only alphabetic characters.

o Example: "hello".isalpha() → True

6. str.isalnum()

o Checks if the string contains only alphanumeric characters.

o Example: "hello123".isalnum() → True

7. str.lstrip()

o Removes leading whitespace (or specified characters).

o Example: " hello".lstrip() → "hello"

8. str.rstrip()

o Removes trailing whitespace (or specified characters).

o Example: "hello ".rstrip() → "hello"

9. str.zfill()

o Pads the string with zeros on the left to fill a specified width.

o Example: "42".zfill(5) → "00042"

10. str.partition()

o Splits the string into three parts: before, delimiter, after.

o Example: "hello world".partition(' ') → ('hello', ' ', 'world')


3. Less Commonly Used String Methods

1. str.swapcase()

o Swaps uppercase to lowercase and vice versa.

o Example: "Hello".swapcase() → "hELLO"

2. str.casefold()

o Converts the string to lowercase (more aggressive than lower()).

o Example: "HELLO".casefold() → "hello"

3. str.index()

o Returns the index of the first occurrence of a substring, raises ValueError if not
found.

o Example: "hello".index('e') → 1

4. str.rfind()

o Finds the last occurrence of a substring or -1 if not found.

o Example: "banana".rfind('a') → 5

5. str.rsplit()

o Splits the string from the right into a list.

o Example: "a,b,c".rsplit(',', 1) → ['a,b', 'c']

6. str.center()

o Centers the string within a specified width.

o Example: "hello".center(10, '-') → "--hello---"

7. str.ljust()

o Left-aligns the string in a field of specified width.

o Example: "hello".ljust(10, '-') → "hello-----"

8. str.rjust()

o Right-aligns the string in a field of specified width.

o Example: "hello".rjust(10, '-') → "-----hello"

9. str.splitlines()

o Splits a string at line breaks.

o Example: "line1\nline2".splitlines() → ['line1', 'line2']

10. str.expandtabs()

o Expands tabs into spaces.

o Example: "hello\tworld".expandtabs(4) → "hello world"


4. Rarely Used String Methods

1. str.islower()

o Checks if all characters are lowercase.

o Example: "hello".islower() → True

2. str.isupper()

o Checks if all characters are uppercase.

o Example: "HELLO".isupper() → True

3. str.isspace()

o Checks if the string contains only whitespace.

o Example: " ".isspace() → True

4. str.isprintable()

o Checks if all characters in the string are printable.

o Example: "hello".isprintable() → True

5. str.isidentifier()

o Checks if the string is a valid Python identifier.

o Example: "variable_name".isidentifier() → True

6. str.maketrans()

o Creates a translation table for str.translate().

o Example: str.maketrans('abc', '123')

7. str.translate()

o Translates the string using a translation table.

o Example: "abc".translate(str.maketrans('abc', '123')) → "123"

8. str.removeprefix() (Python 3.9+)

o Removes a prefix from the string.

o Example: "unhappy".removeprefix('un') → "happy"

9. str.removesuffix() (Python 3.9+)

o Removes a suffix from the string.

o Example: "kindness".removesuffix('ness') → "kind"

10. str.encode()

o Encodes the string into bytes using a specified encoding.

o Example: "hello".encode('utf-8')

You might also like