
Python string is used to enclose a sequence of characters inside double or single quotes. The indexing of a Python string starts with 0 and -1 marks the end. They are immutable data structures that contain various methods used in Python.
In this article, we are going to learn more about Python String and its uses in programming, web development, and other Python projects.
| #Create Python String using Double Quotes str1= “Physics Wallah” #Create Python String using Single Quotes str2= “Physics Wallah” |
| Method | Description |
| str.lower() | Converts all characters to lowercase. |
| str.upper() | Converts all characters to uppercase. |
| str.capitalize() | Capitalizes the first character of the string. |
| str.title() | Converts the first character of each word to uppercase. |
| str.strip() | Removes leading and trailing whitespace (or specified characters). |
| str.lstrip() | Removes leading whitespace (or specified characters). |
| str.rstrip() | Removes trailing whitespace (or specified characters). |
| str.split() | Splits the string into a list based on a delimiter (default: space). |
| str.join(iterable) | Joins elements of an iterable with the string as the separator. |
| str.replace(old, new) | Replaces all occurrences of old with new. |
| str.find(sub) | Returns the lowest index of sub in the string; returns -1 if not found. |
| str.index(sub) | Like find(), but raises a ValueError if sub is not found. |
| str.startswith(prefix) | Returns True if the string starts with the specified prefix. |
| str.endswith(suffix) | Returns True if the string ends with the specified suffix. |
| str.isdigit() | Returns True if the string contains only digits. |
| str.isalpha() | Returns True if the string contains only alphabetic characters. |
| str.isalnum() | Returns True if the string contains only alphanumeric characters (letters and numbers). |
| str.isspace() | Returns True if the string contains only whitespace characters. |
| str.swapcase() | Converts uppercase characters to lowercase and vice versa. |
| str.zfill(width) | Pads the string on the left with zeros to make it the specified width. |
| a = “This is a Python String.” print (a) |
| a = “““ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ””” print (a) |
| a = “Physics Wallah” print (a[2]) |
| text = "Hello WORLD" print(text.lower()) # Output: 'hello world' |
| text = "Hello world" print(text.upper()) # Output: 'HELLO WORLD' |

| text = "hello world" print(text.capitalize()) # Output: 'Hello world' |

| text = "hello world" print(text.title()) # Output: 'Hello World' |

| text = " hello world " print(text.strip()) # Output: 'hello world' text = " hello world" print(text.lstrip()) # Output: 'hello world' text = "hello world " print(text.rstrip()) # Output: 'hello world' |
| words = ['Hello', 'World', 'Python'] print(", ".join(words)) # Output: 'hello, world, python' |

| text = "hello world" print(text.replace("world", "Python")) # Output: 'hello Python' |
| text = "hello world" print(text.find("world")) # Output: 6 print(text.find("Python")) # Output: -1 |
| text = "hello world" print(text.startswith("hello")) # Output: True text = "hello world" print(text.endswith("world")) # Output: True |