|
| 1 | +--- |
| 2 | +sidebar_position: 6 |
| 3 | +--- |
| 4 | + |
| 5 | +# Strings |
| 6 | + |
| 7 | +## Definition of a String |
| 8 | + |
| 9 | +- A **string** is a sequence of characters, essentially text data. In Python, a string is represented by enclosing characters within either **single** (`'`) or **double quotes** (`"`). |
| 10 | +- Example: |
| 11 | + ```python title="strings.py" |
| 12 | + name = "John" |
| 13 | + greeting = 'Hello, world!' |
| 14 | + ``` |
| 15 | + |
| 16 | +## Creating Strings |
| 17 | + |
| 18 | +- Strings can be assigned to variables: |
| 19 | + ```python title="strings.py" |
| 20 | + city = "San Francisco" |
| 21 | + ``` |
| 22 | +- **Double vs. Single Quotes**: |
| 23 | + - You can use both single or double quotes to create strings. |
| 24 | + - The opening and closing quotes must match. |
| 25 | + - Example: |
| 26 | + ```python title="strings.py" |
| 27 | + sentence = 'It\'s a sunny day' |
| 28 | + # Alternatively, using double quotes avoids the need to escape the single quote: |
| 29 | + sentence = "It's a sunny day" |
| 30 | + ``` |
| 31 | + |
| 32 | +## Multi-line Strings |
| 33 | + |
| 34 | +- For **multi-line strings**, use **triple quotes** (`'''` or `"""`). |
| 35 | +- Example: |
| 36 | + ```python title="strings.py" |
| 37 | + address = """123 Elm Street |
| 38 | + Springfield |
| 39 | + USA""" |
| 40 | + ``` |
| 41 | +- This allows the string to span multiple lines. |
| 42 | +
|
| 43 | +## String Concatenation |
| 44 | +
|
| 45 | +- You can **concatenate** (join) strings using the **`+` operator**: |
| 46 | + ```python title="strings.py" |
| 47 | + full_name = "John" + " " + "Doe" |
| 48 | + # Output: "John Doe" |
| 49 | + ``` |
| 50 | +- Note that **spaces** between words must be explicitly added, as shown. |
| 51 | +
|
| 52 | +## String Length |
| 53 | +
|
| 54 | +- Use the **`len()` function** to get the length of a string: |
| 55 | + ```python title="strings.py" |
| 56 | + length = len(full_name) |
| 57 | + print("Length of the full name:", length) |
| 58 | + # Output: 8 |
| 59 | + ``` |
| 60 | +
|
| 61 | +## String Repetition |
| 62 | +
|
| 63 | +- You can **repeat** a string using the `*` operator: |
| 64 | + ```python title="strings.py" |
| 65 | + dash_line = "-" * 5 |
| 66 | + # Output: "-----" |
| 67 | + ``` |
| 68 | +
|
| 69 | +## String Methods |
| 70 | +
|
| 71 | +- **`upper()`**: Converts all characters to uppercase. |
| 72 | + ```python title="strings.py" |
| 73 | + greeting = "hello" |
| 74 | + print(greeting.upper()) # Output: "HELLO" |
| 75 | + ``` |
| 76 | +- **`lower()`**: Converts all characters to lowercase. |
| 77 | + ```python title="strings.py" |
| 78 | + print(greeting.lower()) # Output: "hello" |
| 79 | + ``` |
| 80 | +- **`title()`**: Converts the first character of each word to uppercase. |
| 81 | + ```python title="strings.py" |
| 82 | + title_greeting = "welcome to python" |
| 83 | + print(title_greeting.title()) # Output: "Welcome To Python" |
| 84 | + ``` |
| 85 | +
|
| 86 | +## Whitespace Removal |
| 87 | +
|
| 88 | +- **`strip()`**: Removes leading and trailing whitespace. |
| 89 | + ```python title="strings.py" |
| 90 | + spaced_string = " Hello " |
| 91 | + print(spaced_string.strip()) # Output: "Hello" |
| 92 | + ``` |
| 93 | +- **`lstrip()`**: Removes leading whitespace only. |
| 94 | +- **`rstrip()`**: Removes trailing whitespace only. |
| 95 | +
|
| 96 | +## Replacing Substrings |
| 97 | +
|
| 98 | +- **`replace(old, new)`**: Replaces all occurrences of `old` with `new` in the string. |
| 99 | + ```python title="strings.py" |
| 100 | + text = "I like apples" |
| 101 | + new_text = text.replace("apples", "bananas") |
| 102 | + print(new_text) # Output: "I like bananas" |
| 103 | + ``` |
| 104 | +
|
| 105 | +## Finding Substrings |
| 106 | +
|
| 107 | +- **`count(sub)`**: Counts how many times a substring appears in the string. |
| 108 | + ```python title="strings.py" |
| 109 | + phrase = "banana" |
| 110 | + print(phrase.count("a")) # Output: 3 |
| 111 | + ``` |
| 112 | +- **`find(sub)`**: Returns the index of the first occurrence of the substring. If not found, returns `-1`. |
| 113 | + ```python title="strings.py" |
| 114 | + print(phrase.find("na")) # Output: 2 |
| 115 | + ``` |
| 116 | +
|
| 117 | +## Accessing Characters in a String |
| 118 | +
|
| 119 | +- You can **access individual characters** in a string using **indexing** with square brackets. |
| 120 | +- **Index starts at `0`** (first character): |
| 121 | + ```python title="strings.py" |
| 122 | + first_char = phrase[0] # Output: 'b' |
| 123 | + ``` |
| 124 | +- **Negative indexing** starts from the end: |
| 125 | + ```python title="strings.py" |
| 126 | + last_char = phrase[-1] # Output: 'a' |
| 127 | + ``` |
| 128 | +
|
| 129 | +## Comments and Disabling Code |
| 130 | +
|
| 131 | +- Use the **`#` symbol** to add comments to your code. |
| 132 | +- Example: |
| 133 | + ```python title="strings.py" |
| 134 | + # This is a comment |
| 135 | + print("Hello, World!") # This line prints a greeting |
| 136 | + ``` |
| 137 | +- You can **comment out lines of code** to prevent them from executing, especially useful during debugging: |
| 138 | + ```python title="strings.py" |
| 139 | + # print("This line won't run") |
| 140 | + print("This line will run") |
| 141 | + ``` |
| 142 | +
|
| 143 | +## String Slicing |
| 144 | +
|
| 145 | +- You can extract **substrings** using slicing syntax `[start:end]`: |
| 146 | + ```python title="strings.py" |
| 147 | + text = "Python programming" |
| 148 | + sub_text = text[0:6] # Output: "Python" |
| 149 | + ``` |
| 150 | +- **`start`** is inclusive, **`end`** is exclusive. |
| 151 | +
|
| 152 | +This overview gives a foundational understanding of working with strings in Python, covering creating, manipulating, and accessing string data effectively. |
0 commit comments