In Python, a string is a sequence of characters enclosed in single quotes (') or double quotes ("). Strings are immutable, meaning their values cannot be changed after creation. Python provides a variety of string functions that can be used to manipulate and work with strings.
Creating Strings:
# Single-quoted string
str1 = 'Hello, World!'
# Double-quoted string
str2 = "Python is awesome!"
# Multi-line string using triple quotes
str3 = '''This is a multi-line
string example.'''
Common String Functions in Python:
len() – Returns the length of the string.
my_string = "Hello"print(len(my_string)) # Output: 5
lower() – Converts all characters in the string to lowercase.
my_string = "Hello"print(my_string.lower()) # Output: hello
upper() – Converts all characters in the string to uppercase.
my_string = "Hello"print(my_string.upper()) # Output: HELLO
strip() – Removes leading and trailing spaces from the string.
my_string = " Hello World! "print(my_string.strip()) # Output: "Hello World!"
replace(old, new) – Replaces occurrences of a substring (old) with another substring (new).
my_string = "Hello World"print(my_string.replace("World", "Python")) # Output: Hello Python
split(separator) – Splits the string into a list of substrings based on the specified separator.
my_string = "Hello, World, Python"print(my_string.split(", ")) # Output: ['Hello', 'World', 'Python']
join(iterable) – Joins elements of an iterable (like a list) into a single string.
words = ['Hello', 'World', 'Python']print(" ".join(words)) # Output: Hello World Python
find(substring) – Returns the index of the first occurrence of the specified substring, or -1 if not found.
my_string = "Hello, World"print(my_string.find("World")) # Output: 7
count(substring) – Returns the number of times the specified substring occurs in the string.
my_string = "Hello, Hello, Hello"print(my_string.count("Hello")) # Output: 3
startswith(prefix) – Checks if the string starts with the specified prefix.
my_string = "Hello World"print(my_string.startswith("Hello")) # Output: True
endswith(suffix) – Checks if the string ends with the specified suffix.
my_string = "Hello World"print(my_string.endswith("World")) # Output: True
capitalize() – Capitalizes the first character of the string.
my_string = "hello"print(my_string.capitalize()) # Output: Hello
title() – Capitalizes the first letter of each word in the string.
my_string = "hello world"print(my_string.title()) # Output: Hello World
isalpha() – Checks if all characters in the string are alphabetic (letters only).
my_string = "Hello"print(my_string.isalpha()) # Output: True
my_string = "Hello123"
print(my_string.isalpha()) # Output: False
isdigit() – Checks if all characters in the string are digits.
my_string = "12345"print(my_string.isdigit()) # Output: True
my_string = "Hello123"
print(my_string.isdigit()) # Output: False
isnumeric() – Similar to isdigit(), checks if all characters are numeric.
my_string = "12345"print(my_string.isnumeric()) # Output: True
isupper() – Checks if all characters in the string are uppercase.
my_string = "HELLO"print(my_string.isupper()) # Output: True
islower() – Checks if all characters in the string are lowercase.
my_string = "hello"print(my_string.islower()) # Output: True
swapcase() – Swaps the case of all characters in the string.
my_string = "Hello World"print(my_string.swapcase()) # Output: hELLO wORLD
Examples:
# Using various string functions
sentence = " Python is amazing! "
# Strip leading/trailing spaces
stripped_sentence = sentence.strip()
# Convert to uppercase
upper_sentence = stripped_sentence.upper()
# Replace a word
new_sentence = upper_sentence.replace("AMAZING", "GREAT")
# Split the string
words = new_sentence.split()
print("Original:", sentence)
print("Stripped:", stripped_sentence)
print("Uppercase:", upper_sentence)
print("Replaced:", new_sentence)
print("Words List:", words)
Key Points:
Strings in Python are immutable, meaning you cannot change them directly after they are created.
String functions like upper(), lower(), replace(), and strip() create new strings but don't modify the original string.
Python has a rich set of built-in functions to manipulate strings easily.
Let me know if you'd like more examples or explanations about any of these functions! 😊
0 Comments