DEV Community

Cover image for Python Strings: A Beginner's Guide
Megalraja
Megalraja

Posted on

Python Strings: A Beginner's Guide

When I started learning Python, one of the first things I learned was how to work with strings.

Strings are used in almost every program, whether we are displaying messages, collecting user input, or working with text.

In this blog, let's understand Python strings with simple examples.

What is a string in Python?

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

language = "Python"
message = 'Hello World'

print(language)
print(message)
Enter fullscreen mode Exit fullscreen mode

Output:

Python
Hello World
Enter fullscreen mode Exit fullscreen mode

1. Accessing characters using indexing

Each character in a string has an index. Python indexing starts from 0, which means the first character is at index 0.

language = "Python"

print(language[0])
print(language[1]) 
print(language[5])
Enter fullscreen mode Exit fullscreen mode

Output:

P
y
n
Enter fullscreen mode Exit fullscreen mode

Python also supports negative indexing, which starts from the end of the string.

language = "Python"

print(language[-1])
print(language[-2])
Enter fullscreen mode Exit fullscreen mode

Output:

n
o
Enter fullscreen mode Exit fullscreen mode

Negative indexing is useful when we want to access characters from the end without knowing the exact length of the string.

2. String slicing

Slicing allows us to extract a part of a string.

The syntax is:

string[start:stop:step]
Enter fullscreen mode Exit fullscreen mode
  • start specifies where slicing begins.
  • stop specifies where slicing ends, but the stop index is not included.
  • step specifies how many positions to move each time.

Example:

language = "Python"

print(language[0:3])
print(language[2:5])
print(language[::2])
Enter fullscreen mode Exit fullscreen mode

Output:

Pyt
tho
Pto
Enter fullscreen mode Exit fullscreen mode

We can also reverse a string using slicing.

language = "Python"

print(language[::-1])
Enter fullscreen mode Exit fullscreen mode

Output:

nohtyP
Enter fullscreen mode Exit fullscreen mode

Here, the step value is -1, so Python moves backward through the string.

3. Useful string methods

Python provides many built-in methods to work with strings.

text = "  hello python  "

print(text.upper())
print(text.lower())
print(text.strip())
print(text.replace("python", "world"))
Enter fullscreen mode Exit fullscreen mode

Output:

  HELLO PYTHON  
  hello python  
hello python
  hello world  
Enter fullscreen mode Exit fullscreen mode

Some useful string methods are:

  • upper() – Converts characters to uppercase.
  • lower() – Converts characters to lowercase.
  • strip() – Removes whitespace from both ends.
  • replace() – Replaces part of a string.
  • split() – Splits a string into a list.
  • find() – Finds the position of a substring.

4. Strings are immutable

One important concept in Python is that strings are immutable.

This means we cannot change an individual character in an existing string.

language = "Python"

language[0] = "J"
Enter fullscreen mode Exit fullscreen mode

This code produces a TypeError.

Instead, we can create a new string:

language = "Python"

language = "J" + language[1:]

print(language)
Enter fullscreen mode Exit fullscreen mode

Output:

Jython
Enter fullscreen mode Exit fullscreen mode

We did not modify the original string. We created a new string and assigned it to the variable.

5. String concatenation

We can combine two or more strings using the + operator.

first_word = "Hello"
second_word = "World"

message = first_word + " " + second_word

print(message)
Enter fullscreen mode Exit fullscreen mode

Output:

Hello World
Enter fullscreen mode Exit fullscreen mode

We can also use f-strings to insert variables into a string.

language = "Python"
level = "Beginner"

print(f"I am learning {language} at a {level} level.")
Enter fullscreen mode Exit fullscreen mode

Output:

I am learning Python at a Beginner level.
Enter fullscreen mode Exit fullscreen mode

F-strings make it easier to create readable messages using variables.

Conclusion

Python strings are simple to understand, but they are also very powerful.

Learning indexing, slicing, string methods, and immutability helps us work with text more confidently.

Top comments (0)