---
title: 4.1 String Basics
description: "A **string** is a sequence of characters enclosed within single quotes (`' '`), double quotes (`\" \"`), or triple quotes (`''' '''` or `\"\"\" \"\"\"`). Strings are one of the most commonly used data types in Python. They are used…"
url: "https://edcret.in/docs/python-programming/strings-python/4-1-string-basics/"
updated: "2026-07-29"
category: Python Programming / Strings
---

# 4.1 String Basics

A **string** is a sequence of characters enclosed within single quotes (`' '`), double quotes (`" "`), or triple quotes (`''' '''` or `""" """`).

Strings are one of the most commonly used data types in Python. They are used to store and manipulate text such as names, addresses, messages, passwords, email IDs, and many other forms of textual information.

Every character in a string, including letters, digits, spaces, and special symbols, is stored in a specific order. Python treats all of these as characters within the string.

---

## Syntax

```
string_name = "text"
```

or

```
string_name = 'text'
```

### Components

| Component | Description |
| --- | --- |
| `string_name` | Name of the variable that stores the string. |
| `=` | Assignment operator. |
| `" "` or `' '` | Quotes used to define the string. |
| `text` | Sequence of characters stored in the string. |

---

## Example 1: Creating a String Using Double Quotes

**main.py**

```
message = "Hello, Python!"

print(message)
print(type(message))
```

### Output

```
Hello, Python!
<class 'str'>
```

### Explanation

- A string is assigned to the variable `message`.
- The `print()` function displays the string.
- `type()` confirms that the data type is `str`.

---

## Example 2: Creating a String Using Single Quotes

**main.py**

```
language = 'Python'

print(language)
```

### Output

```
Python
```

### Explanation

- Python allows strings to be enclosed in single quotes.
- Single and double quotes are functionally equivalent.
- Choose one style and use it consistently.

---

## Example 3: String Containing Numbers

**main.py**

```
value = "12345"

print(value)
print(type(value))
```

### Output

```
12345
<class 'str'>
```

### Explanation

- Although the string contains digits, it is still text.
- Python stores it as a string, not as an integer.
- Arithmetic operations cannot be performed directly on this value.

---

## Example 4: String Containing Special Characters

**main.py**

```
password = "@Python#2026"

print(password)
```

### Output

```
@Python#2026
```

### Explanation

- A string can contain letters, digits, and special characters.
- Python stores every character exactly as written.

---

## Example 5: String with Spaces

**main.py**

```
sentence = "Python is easy to learn."

print(sentence)
```

### Output

```
Python is easy to learn.
```

### Explanation

- Spaces are treated as normal characters.
- They become part of the stored string.
- Python preserves all spaces inside the quotes.

---

## Example 6: Empty String

**main.py**

```
text = ""

print(text)
print(type(text))
```

### Output

```
<class 'str'>
```

### Explanation

- An empty string contains no characters.
- It is still a valid string object.
- Its data type is `str`.

---

## Example 7: Assigning User Input to a String

**main.py**

```
name = input("Enter your name: ")

print(name)
print(type(name))
```

### Sample Input

```
Alice
```

### Output

```
Alice
<class 'str'>
```

### Explanation

- `input()` always returns a string.
- Even if the user enters numbers, the returned value is of type `str`.
- No conversion is needed when reading text.

---

## Example 8: Assigning One String to Another Variable

**main.py**

```
first = "Python"
second = first

print(first)
print(second)
```

### Output

```
Python
Python
```

### Explanation

- The value stored in `first` is assigned to `second`.
- Both variables refer to the same string value.
- Printing either variable displays the same text.

---

## Example 9: Multiple String Variables

**main.py**

```
first_name = "John"
last_name = "Doe"

print(first_name)
print(last_name)
```

### Output

```
John
Doe
```

### Explanation

- Multiple string variables can be created independently.
- Each variable stores its own string value.

---

## Example 10: Different Data Types

**main.py**

```
name = "Alice"
age = 21
height = 5.4

print(type(name))
print(type(age))
print(type(height))
```

### Output

```
<class 'str'>
<class 'int'>
<class 'float'>
```

### Explanation

- `"Alice"` is stored as a string.
- `21` is stored as an integer.
- `5.4` is stored as a floating-point number.
- Python automatically assigns the appropriate data type.

---

# Common Mistakes

### 1. Forgetting Quotes Around Text

**Incorrect**

```
name = Python
```

### Output

```
NameError: name 'Python' is not defined
```

**Reason**

Without quotes, Python treats `Python` as a variable name rather than a string.

**Correct**

```
name = "Python"
```

---

### 2. Mixing Quote Types Incorrectly

**Incorrect**

```
text = "Python'
```

### Output

```
SyntaxError: unterminated string literal
```

**Reason**

A string must begin and end with the same type of quotation mark.

**Correct**

```
text = "Python"
```

or

```
text = 'Python'
```

---

### 3. Assuming Numeric Strings Are Numbers

**Incorrect**

```
value = "100"

print(value + 50)
```

### Output

```
TypeError: can only concatenate str (not "int") to str
```

**Reason**

`"100"` is a string, not an integer.

**Correct**

```
value = int("100")

print(value + 50)
```

---

### 4. Forgetting That `input()` Returns a String

**Incorrect**

```
age = input("Enter age: ")

print(age + 5)
```

### Output

```
TypeError: can only concatenate str (not "int") to str
```

**Reason**

`input()` always returns a string.

**Correct**

```
age = int(input("Enter age: "))

print(age + 5)
```

---

# Best Practices

- Use meaningful variable names for string values.
- Choose either single or double quotes and use them consistently.
- Use double quotes when the string contains a single quote, and vice versa.
- Remember that `input()` always returns a string.
- Convert numeric strings to numeric types before performing arithmetic operations.

---

# Key Points to Remember

- A string is a sequence of characters enclosed in quotes.
- Python uses the `str` data type to represent strings.
- Strings can contain letters, digits, spaces, and special characters.
- Single quotes and double quotes both create strings.
- An empty string is a valid string.
- `input()` always returns a string.
- Numeric strings are text, not numbers.
