|
| 1 | +--- |
| 2 | +id: python-string |
| 3 | +title: String in Python |
| 4 | +sidebar_label: String in Python #displays in sidebar |
| 5 | +sidebar_position: 7 |
| 6 | +tags: |
| 7 | + [ |
| 8 | + Python, |
| 9 | + Introduction of python, |
| 10 | + Python Syntax, |
| 11 | + Variables, |
| 12 | + Operators, |
| 13 | + Type Casting, |
| 14 | + String |
| 15 | + ] |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +In Python, a **string** is a sequence of characters enclosed within **single (`'`)**, **double (`"`)**, or **triple quotes (`''' '''` or `""" """`)**. |
| 20 | +It is used to store and manipulate **textual data**. |
| 21 | + |
| 22 | +```python |
| 23 | +str1 = 'Hello' |
| 24 | +str2 = "World" |
| 25 | +str3 = '''This is a multi-line string.''' |
| 26 | +```` |
| 27 | + |
| 28 | +## Creating Strings |
| 29 | + |
| 30 | +Strings can be created in several ways: |
| 31 | + |
| 32 | +```python |
| 33 | +name = "Dhruba" |
| 34 | +message = 'Welcome to Python' |
| 35 | +multiline = """This |
| 36 | +is a |
| 37 | +multiline string.""" |
| 38 | +``` |
| 39 | + |
| 40 | +## String Indexing and Slicing |
| 41 | + |
| 42 | +**Indexing**: Access characters by position (starting at index 0). |
| 43 | + |
| 44 | +```python |
| 45 | +text = "Python" |
| 46 | +print(text[0]) # P |
| 47 | +print(text[-1]) # n |
| 48 | +``` |
| 49 | + |
| 50 | +**Slicing**: Extract a part of the string. |
| 51 | + |
| 52 | +```python |
| 53 | +print(text[0:3]) # Pyt |
| 54 | +print(text[::2]) # Pto |
| 55 | +print(text[1:-1]) # ytho |
| 56 | +``` |
| 57 | + |
| 58 | +## String Methods |
| 59 | + |
| 60 | +| Method | Description | |
| 61 | +| ----------------- | ------------------------------------ | |
| 62 | +| `upper()` | Converts all characters to uppercase | |
| 63 | +| `lower()` | Converts all characters to lowercase | |
| 64 | +| `strip()` | Removes spaces from both ends | |
| 65 | +| `replace(a, b)` | Replaces `a` with `b` | |
| 66 | +| `startswith(val)` | Checks if string starts with `val` | |
| 67 | +| `endswith(val)` | Checks if string ends with `val` | |
| 68 | +| `find(val)` | Finds the first index of `val` | |
| 69 | +| `count(val)` | Counts occurrences of `val` | |
| 70 | + |
| 71 | +```python |
| 72 | +msg = " Hello Python " |
| 73 | +print(msg.upper()) # HELLO PYTHON |
| 74 | +print(msg.strip()) # Hello Python |
| 75 | +print(msg.replace("Python", "JS")) # Hello JS |
| 76 | +``` |
| 77 | + |
| 78 | +## String Concatenation and Repetition |
| 79 | + |
| 80 | +**Concatenation** with `+`: |
| 81 | + |
| 82 | +```python |
| 83 | +first = "Hello" |
| 84 | +second = "World" |
| 85 | +print(first + " " + second) # Hello World |
| 86 | +``` |
| 87 | + |
| 88 | +**Repetition** with `*`: |
| 89 | + |
| 90 | +```python |
| 91 | +print("Hi! " * 3) # Hi! Hi! Hi! |
| 92 | +``` |
| 93 | + |
| 94 | +## Using `in` and `not in` Operators |
| 95 | + |
| 96 | +Check for substring presence: |
| 97 | + |
| 98 | +```python |
| 99 | +text = "Python is fun" |
| 100 | +print("fun" in text) # True |
| 101 | +print("Java" not in text) # True |
| 102 | +``` |
| 103 | + |
| 104 | +## String Formatting |
| 105 | + |
| 106 | +### f-string (Python 3.6+) |
| 107 | + |
| 108 | +```python |
| 109 | +name = "Dhruba" |
| 110 | +age = 22 |
| 111 | +print(f"My name is {name} and I am {age} years old.") |
| 112 | +``` |
| 113 | + |
| 114 | +### format() method |
| 115 | + |
| 116 | +```python |
| 117 | +print("My name is {} and I am {} years old.".format(name, age)) |
| 118 | +``` |
| 119 | + |
| 120 | +### % operator |
| 121 | + |
| 122 | +```python |
| 123 | +print("My name is %s and I am %d years old." % (name, age)) |
| 124 | +``` |
| 125 | + |
| 126 | +--- |
| 127 | + |
| 128 | +## Escape Sequences |
| 129 | + |
| 130 | +Escape characters add special formatting in strings: |
| 131 | + |
| 132 | +| Escape | Meaning | |
| 133 | +| ------ | ------------ | |
| 134 | +| `\n` | New line | |
| 135 | +| `\t` | Tab space | |
| 136 | +| `\\` | Backslash | |
| 137 | +| `\'` | Single quote | |
| 138 | +| `\"` | Double quote | |
| 139 | + |
| 140 | +```python |
| 141 | +print("Hello\nWorld") # Line break |
| 142 | +print("Name:\tDhruba") # Tab |
| 143 | +``` |
| 144 | + |
| 145 | + |
| 146 | +## Multiline Strings |
| 147 | + |
| 148 | +Triple quotes allow multi-line text: |
| 149 | + |
| 150 | +```python |
| 151 | +message = """This is line 1 |
| 152 | +This is line 2 |
| 153 | +This is line 3""" |
| 154 | +print(message) |
| 155 | +``` |
| 156 | + |
| 157 | + |
| 158 | +## Use Cases and Examples |
| 159 | + |
| 160 | +### Greet user |
| 161 | + |
| 162 | +```python |
| 163 | +name = input("Enter your name: ") |
| 164 | +print(f"Welcome, {name}!") |
| 165 | +``` |
| 166 | + |
| 167 | +### Count letters |
| 168 | + |
| 169 | +```python |
| 170 | +text = "banana" |
| 171 | +print(text.count("a")) # 3 |
| 172 | +``` |
| 173 | + |
| 174 | +### Read file and process |
| 175 | + |
| 176 | +```python |
| 177 | +with open("file.txt") as f: |
| 178 | + data = f.read() |
| 179 | + print(data.lower()) |
| 180 | +``` |
| 181 | + |
| 182 | +### Validate email domain |
| 183 | + |
| 184 | +```python |
| 185 | + |
| 186 | +if email.endswith("@example.com"): |
| 187 | + print("Valid domain") |
| 188 | +``` |
| 189 | + |
| 190 | + |
| 191 | +## Summary |
| 192 | + |
| 193 | +* Strings are **immutable** sequences of characters. |
| 194 | +* Support **indexing**, **slicing**, **concatenation**, and **repetition**. |
| 195 | +* Useful **methods** help in text processing. |
| 196 | +* Use **escape sequences** for formatting. |
| 197 | +* Use **f-strings** or `format()` for clean formatting. |
0 commit comments