DEV Community

Cover image for Python Programming for Beginners – Day 10
augustineowino357-design
augustineowino357-design

Posted on

Python Programming for Beginners – Day 10

Strings in Python
In the previous lesson, we learned about Tuples, Sets, and Dictionaries in Python. Today, we will learn about Strings, one of the most important and commonly used data types in Python programming.

Strings are used to store and manipulate text data such as names, messages, passwords, and sentences.

What is a String?

A String is a sequence of characters enclosed inside:

  • Single quotes "' '"
  • Double quotes "" ""
  • Triple quotes "''' '''" or """" """"

Example

name = "Python Programming"
print(name)
Enter fullscreen mode Exit fullscreen mode

Output

Python Programming
Enter fullscreen mode Exit fullscreen mode

Creating Strings

Single Quotes

language = 'Python'
Enter fullscreen mode Exit fullscreen mode

Double Quotes

course = "Programming"
Enter fullscreen mode Exit fullscreen mode

Triple Quotes

Triple quotes are used for multi-line strings.

message = """
Welcome to Python Programming
"""
Enter fullscreen mode Exit fullscreen mode

Accessing Characters in a String

Each character in a string has an index number.

Python indexing starts from "0".

Example

text = "Python"

print(text[0])
print(text[1])
Enter fullscreen mode Exit fullscreen mode

Output

P
y
Enter fullscreen mode Exit fullscreen mode

Negative Indexing

Negative indexing accesses characters from the end.

Example

text = "Python"

print(text[-1])
Enter fullscreen mode Exit fullscreen mode

Output

n
Enter fullscreen mode Exit fullscreen mode

String Length

The "len()" function returns the number of characters in a string.

Example

text = "Python"

print(len(text))
Enter fullscreen mode Exit fullscreen mode

Output

6
Enter fullscreen mode Exit fullscreen mode

String Slicing

String slicing extracts part of a string.

Example

text = "Python Programming"

print(text[0:6])
Enter fullscreen mode Exit fullscreen mode

Output

Python
Enter fullscreen mode Exit fullscreen mode

String Concatenation

Concatenation means joining strings together.

Example

first_name = "Augustine"
last_name = "Owino"

full_name = first_name + " " + last_name

print(full_name)
Enter fullscreen mode Exit fullscreen mode

Output

Augustine Owino
Enter fullscreen mode Exit fullscreen mode

String Repetition

The "*" operator repeats strings.

Example

text = "Python "

print(text * 3)
Enter fullscreen mode Exit fullscreen mode

Output

Python Python Python
Enter fullscreen mode Exit fullscreen mode

Common String Methods

Python provides many built-in methods for working with strings.

upper()

Converts text to uppercase.

Example

text = "python"

print(text.upper())
Enter fullscreen mode Exit fullscreen mode

Output

PYTHON
Enter fullscreen mode Exit fullscreen mode

lower()

Converts text to lowercase.

Example

text = "PYTHON"

print(text.lower())
Enter fullscreen mode Exit fullscreen mode

Output

python
Enter fullscreen mode Exit fullscreen mode

strip()

Removes spaces from the beginning and end.

Example

text = " Python "

print(text.strip())
Enter fullscreen mode Exit fullscreen mode

Output

Python
Enter fullscreen mode Exit fullscreen mode

replace()

Replaces part of a string.

Example

text = "Python Programming"

print(text.replace("Python", "Java"))
Enter fullscreen mode Exit fullscreen mode

Output

Java Programming
Enter fullscreen mode Exit fullscreen mode

split()

Splits a string into a list.

Example

text = "Python Java C++"

print(text.split())
Enter fullscreen mode Exit fullscreen mode

Output

['Python', 'Java', 'C++']
Enter fullscreen mode Exit fullscreen mode

Checking Strings

Python provides methods to check string contents.

Method| Meaning
"isdigit()"| Checks if all characters are numbers
"isalpha()"| Checks if all characters are letters
"isalnum()"| Checks if string contains letters and numbers

Example

text = "Python123"

print(text.isalnum())
Enter fullscreen mode Exit fullscreen mode

Output

True

Escape Characters in Strings

Escape characters are special characters used inside strings.

Escape Character| Meaning
"\n"| New Line
"\t"| Tab Space
"\""| Double Quotes
"\"| Backslash

Example

text = "Python\nProgramming"
print(text)

text = "Python\tProgramming"
print(text)

text = "He said, \"Python is easy\""
print(text)

text = "C:\Users\Augustine"
print(text)

Output

Python
Programming

Python Programming

He said, "Python is easy"

C:\Users\Augustine

String Membership Operators

Membership operators are used to check whether a substring exists inside a string.

Example

text = "Python Programming"

print("Python" in text)
print("Java" in text)

Output

True
False

Looping Through a String

You can use a "for" loop to access each character in a string.

Example

text = "Python"

for letter in text:
print(letter)

Output

P
y
t
h
o
n

String Formatting

String formatting allows us to insert variables into strings.

Example

name = "Augustine"
age = 22

print(f"My name is {name} and I am {age} years old.")

Output

My name is Augustine and I am 22 years old.

Immutability of Strings

Strings are immutable, meaning they cannot be changed after they are created.

Example

text = "Python"

This will cause an error

text[0] = "J"

text = "Jython"
print(text)

Output

Jython

Practical Example

name = input("Enter your name: ")

print("Welcome", name)
print("Your name has", len(name), "characters")
print("Uppercase:", name.upper())
print("Lowercase:", name.lower())

Summary

In this lesson, we learned:

  • What Strings are in Python.
  • How to create Strings using single, double, and triple quotes.
  • How to access characters using indexing.
  • How to use negative indexing.
  • How to find the length of a String using "len()".
  • How to extract parts of a String using slicing.
  • How to join Strings using concatenation.
  • How to repeat Strings using the "*" operator.
  • Common String methods such as "upper()", "lower()", "strip()", "replace()", and "split()".
  • How to check String contents using "isdigit()", "isalpha()", and "isalnum()".
  • How to use escape characters.
  • How to use membership operators ("in").
  • How to loop through a String.
  • How to format Strings using f-strings.
  • Why Strings are immutable.

Conclusion

Strings are one of the most important data types in Python because almost every program works with text data. Understanding how to create, manipulate, format, and analyze Strings is an essential skill for every Python programmer. By mastering Strings, you will be able to build applications that handle user input, process text, manage files, and communicate information effectively. In the next lesson, we will learn about String Operations and Advanced String Methods to further improve our Python programming skills.

Top comments (0)