Strings in Python

Strings in Python are sequences of characters, enclosed in either single (' ') or double (" ") quotes. They support various operations such as concatenation, slicing, and formatting.

  1. Creation: Strings can be created using single or double quotes: name = 'John' or message = "Hello, World!".

  2. Indexing: Characters within a string can be accessed using index notation: print(message[0]) outputs 'H'.

  3. Slicing: Portions of a string can be extracted using slicing notation: print(message[0:5]) outputs 'Hello'.

  4. Concatenation: Strings can be combined using the + operator: greeting = "Hello, " + name.

  5. Formatting: Strings support placeholders for dynamic content: formatted = f"Hello, {name}".

  6. Methods: Python provides built-in methods for string manipulation, like upper(), lower(), split(), strip(), and more.

  7. Immutability: Strings are immutable, meaning their values cannot be changed after creation. However, new strings can be created from existing ones.

Strings are fundamental data types in Python, extensively used for text processing, manipulation, and representation.