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

Python String Methods Reference

This document provides a quick reference for various Python string methods, detailing their functions and providing examples. Key methods include strip(), lower(), upper(), replace(), split(), and checks for character types like isalpha() and isdigit(). Each method is accompanied by a brief description and an illustrative example.
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 views3 pages

Python String Methods Reference

This document provides a quick reference for various Python string methods, detailing their functions and providing examples. Key methods include strip(), lower(), upper(), replace(), split(), and checks for character types like isalpha() and isdigit(). Each method is accompanied by a brief description and an illustrative example.
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

Python String Methods - Quick Reference

strip()

Removes leading and trailing whitespaces or characters.

Example: ' hello '.strip() -> 'hello'

lstrip()

Removes leading whitespaces.

Example: ' hello '.lstrip() -> 'hello '

rstrip()

Removes trailing whitespaces.

Example: ' hello '.rstrip() -> ' hello'

lower()

Converts all characters to lowercase.

Example: 'HELLO'.lower() -> 'hello'

upper()

Converts all characters to uppercase.

Example: 'hello'.upper() -> 'HELLO'

capitalize()

Capitalizes the first letter.

Example: 'python'.capitalize() -> 'Python'

title()

Capitalizes the first letter of each word.

Example: 'hello world'.title() -> 'Hello World'


Python String Methods - Quick Reference

replace(old, new)

Replaces substrings.

Example: 'apple'.replace('a', 'o') -> 'opple'

split(separator)

Splits string into a list.

Example: 'a,b,c'.split(',') -> ['a', 'b', 'c']

join(iterable)

Joins elements with a separator.

Example: ','.join(['a', 'b']) -> 'a,b'

find(sub)

Returns the index of the first occurrence.

Example: 'hello'.find('e') -> 1

startswith(prefix)

Checks if string starts with prefix.

Example: 'hello'.startswith('he') -> True

endswith(suffix)

Checks if string ends with suffix.

Example: 'test.py'.endswith('.py') -> True

isalpha()

Checks if all characters are letters.

Example: 'abc'.isalpha() -> True


Python String Methods - Quick Reference

isdigit()

Checks if all characters are digits.

Example: '123'.isdigit() -> True

isalnum()

Checks if all characters are alphanumeric.

Example: 'abc123'.isalnum() -> True

isspace()

Checks if string contains only whitespace.

Example: ' '.isspace() -> True

You might also like