Python Basics

Course by zooboole,

Last Updated on 2025-02-26 16:14:49

Strings in Python

What are Strings?

In Python, a string is a sequence of characters enclosed in quotes. Strings can be created using:

  • Single quotes ('Hello')
  • Double quotes ("Hello")
  • Triple quotes ('''Hello''' or """Hello""") for multi-line strings.

Example:

string1 = 'Hello'
string2 = "Hello"
string3 = '''This is a 
multiline string'''

print(string1)
print(string2)
print(string3)

String Characteristics

  • Strings are immutable, meaning they cannot be changed after creation.
  • They support indexing and slicing, allowing access to individual characters and substrings.
  • Strings support various built-in methods for manipulation.

Creating Strings

You can create a string by assigning text to a variable:

message = "Python is amazing!"
print(message)

String Concatenation

You can combine strings using the + operator:

greeting = "Hello"
name = "Alice"
message = greeting + ", " + name + "!"
print(message)

Output:

Hello, Alice!

String Repetition

Use the * operator to repeat a string multiple times:

repeat = "ha" * 3
print(repeat)

Output:

hahaha

Escape Characters

We learned that strings can contain special characters such as quotes, slashes, etc. For example in the sentence This is Max's house, the apostrophy need to apear in the string, meanwhile it's the same character that's used to make strings. To avoid Python confusing the special character with the actual aprostrophy, use escape the charater in the string. Escape characters allow special formatting within strings:

  • \n - Newline
  • \t - Tab
  • \\ - Backslash
  • \' - Single quote
  • \" - Double quote

Example:

print("Hello\nWorld")
print("This is a tab:\tTabbed")
print("This is Max\'s house")

Output:

Hello
World
This is a tab:    Tabbed
This is Max's house

Raw Strings

Raw strings ignore escape sequences by using an r before the string:

raw_string = r"C:\new_folder\test.txt"
print(raw_string)

Output:

C:\new_folder\test.txt

This avoids us from writing C:\\new_folder\\test.txt.

Multi-line Strings

Sometimes, the text at hand can be conserable. Displaying in one line could not be a problem for the computer but, for use humans, readability is important. So, we could break it into short sentences. Triple quotes allow us to write multi-line strings:

long_text = '''This is a
multi-line string in Python.'''
print(long_text)

Summary

  • Strings are sequences of characters enclosed in quotes.
  • Strings are immutable and support indexing, slicing, and various built-in methods.
  • You can concatenate, repeat, and format strings for various applications.
  • Escape characters allow special formatting, and raw strings ignore escape sequences.

Exercise: Try It Yourself!

  1. Create a string variable with your name and print it.
  2. Concatenate two strings: "Python" and "Rocks!" and print the result.
  3. Print a string that contains a new line using the \n escape character.
  4. Use the * operator to print "Hello" three times.
  5. Create a raw string to represent a file path in Windows.
  6. Write a multi-line string using triple quotes and print it.

Try these exercises in a Python interpreter and observe the outputs!

In the next lesson, we'll explore string indices and how to access characters within a string!