| sidebar_position | 6 |
|---|
- 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 ("). - Example:
name = "John" greeting = 'Hello, world!'
- Strings can be assigned to variables:
city = "San Francisco"
- Double vs. Single Quotes:
- You can use both single or double quotes to create strings.
- The opening and closing quotes must match.
- Example:
sentence = 'It\'s a sunny day' # Alternatively, using double quotes avoids the need to escape the single quote: sentence = "It's a sunny day"
- For multi-line strings, use triple quotes (
'''or"""). - Example:
address = """123 Elm Street Springfield USA"""
- This allows the string to span multiple lines.
- You can concatenate (join) strings using the
+operator:full_name = "John" + " " + "Doe" # Output: "John Doe"
- Note that spaces between words must be explicitly added, as shown.
- Use the
len()function to get the length of a string:length = len(full_name) print("Length of the full name:", length) # Output: 8
- You can repeat a string using the
*operator:dash_line = "-" * 5 # Output: "-----"
upper(): Converts all characters to uppercase.greeting = "hello" print(greeting.upper()) # Output: "HELLO"
lower(): Converts all characters to lowercase.print(greeting.lower()) # Output: "hello"
title(): Converts the first character of each word to uppercase.title_greeting = "welcome to python" print(title_greeting.title()) # Output: "Welcome To Python"
strip(): Removes leading and trailing whitespace.spaced_string = " Hello " print(spaced_string.strip()) # Output: "Hello"
lstrip(): Removes leading whitespace only.rstrip(): Removes trailing whitespace only.
replace(old, new): Replaces all occurrences ofoldwithnewin the string.text = "I like apples" new_text = text.replace("apples", "bananas") print(new_text) # Output: "I like bananas"
count(sub): Counts how many times a substring appears in the string.phrase = "banana" print(phrase.count("a")) # Output: 3
find(sub): Returns the index of the first occurrence of the substring. If not found, returns-1.print(phrase.find("na")) # Output: 2
- You can access individual characters in a string using indexing with square brackets.
- Index starts at
0(first character):first_char = phrase[0] # Output: 'b'
- Negative indexing starts from the end:
last_char = phrase[-1] # Output: 'a'
- Use the
#symbol to add comments to your code. - Example:
# This is a comment print("Hello, World!") # This line prints a greeting
- You can comment out lines of code to prevent them from executing, especially useful during debugging:
# print("This line won't run") print("This line will run")
- You can extract substrings using slicing syntax
[start:end]:text = "Python programming" sub_text = text[0:6] # Output: "Python"
startis inclusive,endis exclusive.
This overview gives a foundational understanding of working with strings in Python, covering creating, manipulating, and accessing string data effectively.