.png)
Creating a string
To create a string in Python, we simply need to enclose a sequence of characters in quotes. There are two types of quotes that we can use: single quotes and double quotes. Here's an example:
We can also use triple quotes to create multiline strings:
my_string = "Hello, world!"
We can also use triple quotes to create multiline strings:
multiline_string = """ This is a multiline string. """
Accessing characters in a string
We can access individual characters in a string by using indexing. In Python, indexing starts at 0, which means that the first character in a string has an index of 0. Here's an example:
We can also use negative indexing to access characters from the end of the string:
my_string = "Hello, world!" print(my_string[0]) # Output: H
We can also use negative indexing to access characters from the end of the string:
print(my_string[-1]) # Output: !
String slicing
In addition to accessing individual characters in a string, we can also slice a string to extract a substring. String slicing is done by specifying the start and end indices of the substring we want to extract, separated by a colon. Here's an example:
my_string = "Hello, world!" print(my_string[0:5]) # Output: Hello
String methods
Python strings come with a variety of built-in methods that we can use to manipulate them. Here are some of the most commonly used methods:
- len(): returns the length of the string
- lower(): converts all characters in the string to lowercase
- upper(): converts all characters in the string to uppercase
- strip(): removes whitespace from the beginning and end of the string
- replace(): replaces a substring with another substring
- split(): splits the string into a list of substrings based on a specified delimiter
my_string = " Hello, World! " print(len(my_string)) # Output: 18 my_string = my_string.strip() print(my_string) # Output: "Hello, World!" my_string = my_string.lower() print(my_string) # Output: "hello, world!" my_string = my_string.replace("hello", "hi") print(my_string) # Output: "hi, world!" my_list = my_string.split(",") print(my_list) # Output: ["hi", "world!"]
Here are some more string methods in Python:
- str.upper() and str.lower() - These methods convert all characters in a string to upper case or lower case, respectively.
- str.isupper() and str.islower() - These methods return True if all characters in a string are uppercase or lowercase, respectively.
- str.capitalize() - This method capitalizes the first character of a string and converts all other characters to lowercase.
- str.title() - This method capitalizes the first character of each word in a string.
- str.swapcase() - This method swaps the case of all characters in a string (upper case becomes lower case and vice versa).
- str.strip() - This method removes leading and trailing whitespace characters from a string.
- str.lstrip() and str.rstrip() - These methods remove leading or trailing whitespace characters, respectively.
- str.startswith(prefix) and str.endswith(suffix) - These methods return True if a string starts or ends with the specified prefix or suffix, respectively.
- str.count(substring) - This method returns the number of occurrences of a substring in a string.
- str.replace(old, new) - This method replaces all occurrences of the specified old substring with the specified new substring in a string.
In conclusion, Python strings are a fundamental data type that are used extensively in Python programming. By understanding how to create and manipulate strings using methods like indexing, slicing, and built-in string methods, you will be well on your way to writing powerful Python programs.
0 Comments