Strings in Python are immutable sequences of Unicode code points (str). Once created, a string's memory contents cannot be changed in place; operations that modify strings return a newly created string object.
flowchart LR
subgraph Slicing Index Range
i0["Index 0 : 'P'"]
i1["Index 1 : 'y'"]
i2["Index 2 : 't'"]
i3["Index 3 : 'h'"]
i4["Index 4 : 'o'"]
i5["Index 5 : 'n'"]
end
subgraph Negative Indexing
n0["Index -6"]
n1["Index -5"]
n2["Index -4"]
n3["Index -3"]
n4["Index -2"]
n5["Index -1"]
end
string[start:stop:step]start: Inclusive start index (defaults to 0).stop: Exclusive end index (defaults to len(string)).step: Increment step size (negative step reverses direction).from typing import List
def process_text_data(raw_text: str) -> None:
# String Slicing & Reversal
word = "Python"
prefix = word[:2] # 'Py'
suffix = word[2:] # 'thon'
reversed_word = word[::-1] # 'nohtyP'
print(f"Original: {word} | Prefix: {prefix} | Reversed: {reversed_word}")
# Standard String Methods
cleaned_text = raw_text.strip().lower()
tokens: List[str] = cleaned_text.split(",")
formatted_output = " | ".join([t.strip().capitalize() for t in tokens])
print(f"Raw Input: '{raw_text}'")
print(f"Processed Output: '{formatted_output}'")
# UTF-8 Encoding & Decoding
encoded_bytes: bytes = word.encode("utf-8")
decoded_str: str = encoded_bytes.decode("utf-8")
print(f"UTF-8 Bytes: {encoded_bytes} -> Decoded: {decoded_str}")
if __name__ == "__main__":
sample_csv = " apple , banana, CHERRY , date "
process_text_data(sample_csv)
''.join(list) for Efficient Concatenation: Avoid looping with s += item inside loops, as creating new strings in each iteration causes $O(N^2)$ memory overhead. Use str.join() for $O(N)$ speed.f"User: {username}") instead of string concatenation or % formatting.s.replace("a", "b") does not alter s; you must reassign the returned result (s = s.replace(...)).Write a function is_palindrome(text: str) -> bool that strips spaces, converts text to lowercase, and checks if it equals its reverse slice (text[::-1]).
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
You've completed this section! Take a quick 5-question quiz to check your understanding.