The string is a sequence of characters enclosed in a single (‘ ‘) or double (” “) or triple quotes (”’ ”’ or “”” “””).
- Immutable: Once created, strings cannot be modified. Any operation that alters a string will return a new string.
- Indexed: Each character in a string has a unique index (starting at 0 for the first character).
- Iterable: Strings are iterable, meaning you can loop through each character using a for loop.
Creating Strings
# Using single, double, or triple quotes
str1 = 'Hello'
str2 = "World"
str3 = '''Triple quotes are useful for
multi-line strings'''
# Creating Empty string
empty_str_1 = ' '
empty_str_2 = str()
print(str1) # Output: Hello
print(str2) # Output: World
print(str3) # Output: Triple quotes are useful for multi-line strings
print(empty_str_1) # Output: = ' '
print(empty_str_2) # Output: = ' '
String Indexing and Slicing
course = "Python"
# Indexing
print(course[0]) # Output: P
print(course[-1]) # Output: n (last character)
# Slicing
print(course[1:4]) # Output: yth (from index 1 to 3)
print(course[:3]) # Output: Pyt (from start to index 2)
print(course[2:]) # Output: thon (from index 2 to the end)
print(course[::-1]) # Output: nohtyP (reversed string)
Example: Reverse a String:
s = "advanced"
reversed_s = s[::-1]
print(reversed_s) # Output: "decnavda"
Mathematical Operators for String
In Python, you can use certain operators with strings:
- Concatenation (+): Combines two strings into one.
- Repetition (*): Duplicates the string a given number of times.
Example:
print("KSR" + " DATAVIZON") # Result: KSR DATAVIZON
print("KSR" * 3) # Result: KSRKSRKSR
Important points:
- The + operator can only concatenate if both values are strings.
- The * operator works when one value is a string and the other is an integer.
Common Functions for Strings
Function | Definition |
len() | Returns the number of characters in a string. |
min() | Returns the smallest character in a string based on Unicode. |
max() | Returns the largest character in a string based on Unicode. |
reversed() | Returns an iterator that accesses the string in reverse order. |
sorted() | Returns a sorted list of the characters in a string. |
Example:
hw = "Hello, World!"
# 1. Length of the string
length = len(hw) # Returns the number of characters
print(f"Length: {length}")
# 2. Minimum character
smallest_char = min(hw) # Returns the smallest character based on Unicode
print(f"Smallest character: '{smallest_char}'")
# 3. Maximum character
largest_char = max(hw) # Returns the largest character based on Unicode
print(f"Largest character: '{largest_char}'")
# 4. Reversed string
reversed_string = ''.join(reversed(hw)) # Reverses the string
print(f"Reversed string: '{reversed_string}'")
# 5. Sorted characters
sorted_chars = sorted(hw) # Returns a sorted list of characters
print(f"Sorted characters: {sorted_chars}")
1. Changing Case:
- str.upper(): Converts all letters in the string to uppercase.
- str.lower(): Converts all letters in the string to lowercase.
- str.capitalize(): Capitalizes the first letter of the string.
- str.title(): Capitalizes the first letter of each word in the string.
s = "hello world"
upper_s = s.upper()
lower_s = s.lower()
capitalized_s = s.capitalize()
title_s = s.title()
print(upper_s) # Output: "HELLO WORLD"
print(lower_s) # Output: "hello world"
print(capitalized_s) # Output: "Hello world"
print(title_s) # Output: "Hello World"
2. Stripping Whitespace:
- str.strip(): Removes whitespace from both the beginning and end of the string.
- str.rstrip(): Removes whitespace from the end of the string only.
- str.lstrip(): Removes whitespace from the beginning of the string only
hw = " Hello, World! "
print(hw.strip()) # Output: 'Hello, World!'
print(hw.lstrip()) # Output: 'Hello, World! ' (removes left-side space)
print(hw.rstrip()) # Output: ' Hello, World!' (removes right-side space)
3. Replacing Substrings:
- str.replace(old, new): Replaces all occurrences of the specified old substring with a new substring.
phrase = "I love Python"
new_phrase = phrase.replace("love", "enjoy")
print(new_phrase) # Output: I enjoy Python
4. Splitting and Joining Strings:
- str.split(delim): Splits the string into a list using the specified delimiter.
- str.join(iterable): Joins elements of an iterable (like a list) into a single string, using the string as a separator.
sentence = "Python is fun"
words = sentence.split() # Split by whitespace (default)
print(words) # Output: ['Python', 'is', 'fun']
words_2 = sentence.split('i') # Split by i
print(words_2) # Output: ['Python ', 's fun']
joined = " ".join(words) # Join the list back into a string
print(joined) # Output: Python is fun
5. Finding Substrings
- find(substring): Returns the index of the first occurrence of the substring. Returns -1 if not found.
- rfind(substring): Returns the index of the last occurrence of the substring. Returns -1 if not found.
- find(substring, begin_index, end_index): Searches for the substring within the specified range. Returns -1 if not found.
- index(substring): Similar to find(), but raises a ValueError if the substring is not found.
- rindex(substring): Similar to rfind(), but raises a ValueError if the substring is not found.
s = "Exploring data with Python is fun"
print(s.find("data")) # Output: 10 (First occurrence of "data")
print(s.find("SQL")) # Output: -1 (Since "SQL" is not found)
print(s.find("i")) # Output: 17 (First occurrence of "i")
print(s.rfind("i")) # Output: 28 (Last occurrence of "i")
# Additional examples with a range:
print(s.find('a', 7, 15)) # Output: 10 (First occurrence of 'a' between index 7 and 15) print(s.find('z', 7, 15)) # Output: -1 (Since 'z' is not found in the range)
s2 = "Machine learning with Python is powerful"
print(s2.index("learning")) # Output: 8 (First occurrence of "learning")
print(s2.index("Python")) # Output: 23 (First occurrence of "Python")
print(s2.index("i")) # Output: 4 (First occurrence of "i")
print(s2.rindex("i")) # Output: 34 (Last occurrence of "i")
# Uncommenting the following line will raise a ValueError because "KSR" is not in the string
# print(s.index("KSR")) # ValueError: substring not found
6. Checking if String is Alphabetic or Numeric
- str.isdigit(): Returns True if all characters in the string are digits; otherwise, returns False.
- str.isalpha(): Returns True if all characters in the string are alphabetic; otherwise, returns False.
- str.isalnum(): Returns True if all characters in the string are alphanumeric (contains both letters and numbers); otherwise, returns False
print("Python".isalpha()) # Output: True (all characters are alphabetic)
print("12345".isdigit()) # Output: True (all characters are digits)
print("Python123".isalnum()) # Output: True (all characters are alphanumeric)
7. Counting substring in the given String:
- str.count(substring): Returns the number of non-overlapping occurrences of the specified substring in the string
s = "hellohellohelloworld"
print(s.count('o')) # Output: 4 (Counts occurrences of 'o')
print(s.count('hello')) # Output: 3 (Counts occurrences of 'hello')
print(s.count('l', 5, 15)) # Output: 3 (Counts occurrences of 'l' between index 5 & 15)
8. Alignment Methods for string
- str.center(width) : This method returns a string that is centered within a given width, padded with spaces by default.
- str.ljust(width): This method makes a string left-aligned in a given width, adding spaces to the right.
- str.rjust(width): This method makes a string left-aligned in a given width, adding spaces to the left.
text = "Hello"
# Center the text
centered = text.center(10)
print(f"Centered: '{centered}'") # Output: ' Hello '
# Left-justify the text
left_justified = text.ljust(10)
print(f"Left-justified: '{left_justified}'") # Output: 'Hello '
# Right-justify the text
right_justified = text.rjust(10)
print(f"Right-justified: '{right_justified}'") # Output: ' Hello'
9. Checking starting and ending part of the string:
- s.startswith(substring): Returns True if the string starts with the specified substring; otherwise, returns False.
- s.endswith(substring): Returns True if the string ends with the specified substring; otherwise, returns False.
s = "Hello, world!"
print(s.startswith("Hello")) # Output: True
print(s.startswith("world")) # Output: False
print(s.endswith("world!")) # Output: True
print(s.endswith("Hello")) # Output: False
10. Check type of characters present in a string:
- str.islower(): Returns True if all characters in the string are lowercase letters; otherwise, returns False.
- str.isupper(): Returns True if all characters in the string are uppercase letters; otherwise, returns False.
- str.istitle(): Returns True if the string is in title case (first letter of each word is uppercase); otherwise, returns False.
- str.isspace(): Returns True if the string contains only whitespace characters; otherwise, returns False.
s1 = "hello"
s2 = "WORLD"
s3 = "Hello World"
s4 = " "
s5 = " a "
# Using islower()
print(s1.islower()) # Output: True
print(s2.islower()) # Output: False
# Using isupper()
print(s1.isupper()) # Output: False
print(s2.isupper()) # Output: True
# Using istitle()
print(s3.istitle()) # Output: True
print(s1.istitle()) # Output: False
# Using isspace()
print(s4.isspace()) # Output: True
print(s5.isspace()) # Output: False
String Formatting:
In Python, there are multiple ways to format strings with variable values. Three commonly used methods include the % operator, the str.format() method, and f-strings (introduced in Python 3.6).
1. Using the % Operator
he %
operator is used for formatting strings with placeholders like %s
for strings, %d
for integers, and %f
for floats.
name = 'Alice'
age = 30
height = 5.6
formatted_string = "%s is %d years old and %.1f feet tall." % (name, age, height)
print(formatted_string) # Output: Alice is 30 years old and 5.6 feet tall.
Placeholders:
- %s for strings
- %d for integers
- %f for floating-point numbers (use %.nf for n decimal places)
2. Using the format() Method
The str.format() method offers greater flexibility and readability in string formatting. You can use curly braces {} as placeholders and call the format() method with the values you want to insert.
name = 'Alice'
product = 'Smartphone'
price = 499.99
quantity = 3
formatted_string = "{} costs ${:.2f} and there are {} available.".format(product, price, quantity)
print(formatted_string) # Output: Smartphone costs $499.99 and there are 3 available.
Key Features:
- You can use indexed placeholders {0}, {1}, etc., for ordering.
- Named placeholders can also be used for clarity: {“name”: name}.
3. Using f-Strings (Python 3.6+)
f-strings offer a modern way to format strings, allowing for inline expressions and variable references directly within the string.
name = 'Bob'
salary = 75000
years = 5
formatted_string = f"{name} earns ${salary} per year and has been working for {years} years."
print(formatted_string) # Output: Bob earns $75000 per year and has been working for 5 years.
Advantages:
- Readable and concise.
- Supports expressions directly inside the curly braces.
Conclusion
Strings are a fundamental part of Python, offering rich functionality for manipulation and processing. With various built-in methods, Python strings provide the tools needed for formatting, slicing, splitting, and transforming text data, making them essential in most applications.
Knowledge Check
Related Article No.4

Check out our Trending Courses Demo Playlist
Data Analytics with Power Bi and Fabric |
Could Data Engineer |
Data Analytics With Power Bi Fabic |
AWS Data Engineering with Snowflake |
Azure Data Engineering |
Azure & Fabric for Power bi |
Full Stack Power Bi |
Kick Start Your Career With Our Data Job
Social Media channels
► KSR Datavizon Website :- https://www.datavizon.com
► KSR Datavizon LinkedIn :- https://www.linkedin.com/company/datavizon/
► KSR Datavizon You tube :- https://www.youtube.com/c/KSRDatavizon
► KSR Datavizon Twitter :- https://twitter.com/ksrdatavizon
► KSR Datavizon Instagram :- https://www.instagram.com/ksr_datavision
► KSR Datavizon Face book :- https://www.facebook.com/KSRConsultingServices
► KSR Datavizon Playstore :- https://play.google.com/store/apps/details?id=com.datavizon.courses&hl=en-IN
► KSR Datavizon Appstore :- https://apps.apple.com/in/app/ksr-datavizon/id1611034268
Most Commented