Wednesday, 23 July 2025

Python String methods

 Methods : 

s = "hello world"


s.upper()             # 'HELLO WORLD'

s.lower()             # 'hello world'

s.capitalize()        # 'Hello world'

s.title()             # 'Hello World'

s.strip()             # Remove whitespace

s.lstrip()            # Left-strip

s.rstrip()            # Right-strip

s.replace("l", "x")   # 'hexxo worxd'

s.split(" ")          # ['hello', 'world']

s.join(['a', 'b'])    # 'a b'

s.startswith("he")    # True

s.endswith("ld")      # True

s.find("o")           # First index (4)

s.rfind("o")          # Last index (7)

s.count("l")          # 3

s.isalpha()           # False

s.isdigit()           # False

s.isalnum()           # False

s.islower()           # True

s.isupper()           # False

s.swapcase()          # 'HELLO WORLD'

s.zfill(12)           # '00hello world'

s.center(20, "-")     # '----hello world-----'


No comments:

Post a Comment

Python Tuple , Set and Dict

 Tuple Methods : t = (1, 2, 3) t.count(2)            # 1 t.index(3)            # 2 #Set methods : s = {1, 2, 3} s.add(4)              # {1, ...