Skip to main content

Hey everyone! Today we're going to explore one of the most commonly used data types in Python – strings. If you're just starting with Python or need a quick refresher, this guide will help you understand how strings work in Python. Let's get started!

Python Strings

  • A string in Python is a sequence of characters enclosed within single quotes, double quotes, or triple quotes.

  • Strings are immutable, meaning once created, they cannot be changed.

1. Defining a String

Let's see how you can define a string in Python:

name = 'Python'                           # Define a string with single quotes  
greeting = "Hello, World!" # Define a string with double quotes
long_string = '''This is a long string # Define a multi-line string with triple quotes
that spans multiple lines.''' # Continuing the multi-line string

2. Accessing Characters in a String

You can access individual characters in a string using indexing:

name = 'Python'                           # Define a string  
first_char = name[0] # Access the first character: 'P'
last_char = name[-1] # Access the last character: 'n'
print(first_char) # Output: 'P'
print(last_char) # Output: 'n'

3. Slicing a String

You can slice a string to get a substring:

name = 'Python'                           # Define a string  
sub_str = name[0:3] # Slice from index 0 to 2: 'Pyt'
print(sub_str) # Output: 'Pyt'

4. String Concatenation

You can combine two or more strings using the + operator:

first_name = 'John'                       # Define a first name string  
last_name = 'Doe' # Define a last name string
full_name = first_name + ' ' + last_name # Concatenate strings with a space
print(full_name) # Output: 'John Doe'

5. String Formatting

You can format strings using f-strings, which is an efficient and readable way to include variables in a string:

name = 'Alice'                            # Define a name string  
age = 30 # Define an age variable
message = f'{name} is {age} years old.' # Format string with variables
print(message) # Output: 'Alice is 30 years old.'

6. Common String Methods

Python provides several built-in methods to work with strings. Here are a few commonly used ones:

text = '  Hello, Python!  '               # Define a string with leading and trailing spaces  
trimmed_text = text.strip() # Remove leading and trailing spaces: 'Hello, Python!'
uppercase_text = text.upper() # Convert the string to uppercase: ' HELLO, PYTHON! '
lowercase_text = text.lower() # Convert the string to lowercase: ' hello, python! '
replaced_text = text.replace('Python', 'World') # Replace 'Python' with 'World': ' Hello, World! '
print(trimmed_text) # Output: 'Hello, Python!'
print(uppercase_text) # Output: ' HELLO, PYTHON! '
print(lowercase_text) # Output: ' hello, python! '
print(replaced_text) # Output: ' Hello, World! '

7. String Length

You can get the length of a string using the len() function:

text = 'Hello, Python!'                   # Define a string  
length = len(text) # Get the length of the string: 13
print(length) # Output: 13