Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
311 views1 page

Python String Methods Overview

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)
311 views1 page

Python String Methods Overview

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

■ Python String Methods Cheat Sheet

■ Basic Methods
Method Description Example

[Link]() Makes all letters lowercase "HELLO".lower() → 'hello'


[Link]() Makes all letters uppercase "hello".upper() → 'HELLO'
[Link]() Capitalizes first letter only "python".capitalize() → 'Python'
[Link]() Capitalizes first letter of each word "hello world".title() → 'Hello World'
[Link]() Removes spaces at beginning & end " hi ".strip() → 'hi'

■ Searching / Checking
Method Description Example

[Link](x) Checks if string starts with x "hello".startswith("he") → True


[Link](x) Checks if string ends with x "hello".endswith("lo") → True
[Link](x) Finds position of x (-1 if not found) "hello".find("l") → 2
[Link](x) Counts how many times x appears "hello".count("l") → 2
[Link]() Checks if all characters are numbers "123".isdigit() → True
[Link]() Checks if all are letters "abc".isalpha() → True

■ Modifying Strings
Method Description Example

[Link](a, b) Replaces a with b "hello".replace("l", "z") → 'hezzo'


[Link](x) Splits string by x into a list "a,b,c".split(",") → ['a', 'b', 'c']
[Link](list) Joins list into one string ".".join(['a', 'b', 'c']) → 'a.b.c'
[Link](n) Pads with zeros on the left "7".zfill(3) → '007'
[Link](n) Centers string in n spaces "hi".center(5) → ' hi '

Example:
s = " hello world " print([Link]().upper().replace("WORLD", "Python")) # Output: HELLO PYTHON

You might also like