Strings
Definition of a String
- 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:
strings.py
name = "John"
greeting = 'Hello, world!'
Creating Strings
- Strings can be assigned to variables:
strings.py
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:
strings.py
sentence = 'It\'s a sunny day'
# Alternatively, using double quotes avoids the need to escape the single quote:
sentence = "It's a sunny day"
Multi-line Strings
- For multi-line strings, use triple quotes (
'''or"""). - Example:
strings.py
address = """123 Elm Street
Springfield
USA""" - This allows the string to span multiple lines.
String Concatenation
- You can concatenate (join) strings using the
+operator:strings.pyfull_name = "John" + " " + "Doe"
# Output: "John Doe" - Note that spaces between words must be explicitly added, as shown.
String Length
- Use the
len()function to get the length of a string:strings.pylength = len(full_name)
print("Length of the full name:", length)
# Output: 8
String Repetition
- You can repeat a string using the
*operator:strings.pydash_line = "-" * 5
# Output: "-----"
String Methods
upper(): Converts all characters to uppercase.strings.pygreeting = "hello"
print(greeting.upper()) # Output: "HELLO"lower(): Converts all characters to lowercase.strings.pyprint(greeting.lower()) # Output: "hello"title(): Converts the first character of each word to uppercase.strings.pytitle_greeting = "welcome to python"
print(title_greeting.title()) # Output: "Welcome To Python"
Whitespace Removal
strip(): Removes leading and trailing whitespace.strings.pyspaced_string = " Hello "
print(spaced_string.strip()) # Output: "Hello"lstrip(): Removes leading whitespace only.rstrip(): Removes trailing whitespace only.
Replacing Substrings
replace(old, new): Replaces all occurrences ofoldwithnewin the string.strings.pytext = "I like apples"
new_text = text.replace("apples", "bananas")
print(new_text) # Output: "I like bananas"
Finding Substrings
count(sub): Counts how many times a substring appears in the string.strings.pyphrase = "banana"
print(phrase.count("a")) # Output: 3find(sub): Returns the index of the first occurrence of the substring. If not found, returns-1.strings.pyprint(phrase.find("na")) # Output: 2
Accessing Characters in a String
- You can access individual characters in a string using indexing with square brackets.
- Index starts at
0(first character):strings.pyfirst_char = phrase[0] # Output: 'b' - Negative indexing starts from the end:
strings.py
last_char = phrase[-1] # Output: 'a'
Comments and Disabling Code
- Use the
#symbol to add comments to your code. - Example:
strings.py
# 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:
strings.py
# print("This line won't run")
print("This line will run")
String Slicing
- You can extract substrings using slicing syntax
[start:end]:strings.pytext = "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.