Skip to content

Latest commit

 

History

History
50 lines (38 loc) · 1.59 KB

File metadata and controls

50 lines (38 loc) · 1.59 KB

String

A string is an immutable sequence of characters. In Python, strings support indexing, slicing, and a rich set of built-in methods.

Complexity

Operation Time
Index / Slice O(1) / O(k)
Search (in, find) O(n)
Concatenation O(n + m)
Split / Join O(n)

Python Usage

# Slicing
text = "PythonProgramming"
text[:6]     # "Python"
text[-11:]   # "Programming"

# Case
"hello".upper(); "HELLO".lower()

# Find & Replace
"hello".find("ll")                       # 2
"hello".replace("ll", "rr")             # "herro"

# Split & Join
"a,b,c".split(",")                      # ['a', 'b', 'c']
"-".join(['a', 'b', 'c'])               # "a-b-c"

# Substring checks
"Python" in "Hello, Python!"            # True
"banana".count("an")                     # 2

# Formatting
f"My name is {name} and I am {age} years old."

Related LeetCode Questions