String Data Type
Last Updated: 13th August 2025
- A string is a sequence of characters (letters, numbers, symbols, spaces).
- In Python, strings are immutable → once created, you can’t change them directly.
- Strings are always inside quotes:
- Single quotes → 'Hello'
- Double quotes → "Hello"
- Triple quotes → '''Hello''' or """Hello""" (used for multi-line text)
- String is iterable → you can loop through characters in a string or access characters one by one.
Hinglish Tip 🗣: Python me jab bhi koi text store karna ho — jaise naam, address, email, message — toh hum usse quotes me likhte hain. Triple quotes ka use multi-line text ke liye hota hai.
✏ Creating a String
name = "Sadhu"
greeting = 'Hello World'
multi_line = """This is
a multi-line
string."""
# Print the strings
print(name)
print(greeting)
print(multi_line)
# Data type check
print(type(name))
print(type(greeting))
print(type(multi_line))
💡 Here we have created 3 different strings using different types of quotes.We use print function to print them and check their data type.Your output should look like this:
Sadhu
Hello World
This is
a multi-line
string.
<class 'str'>
<class 'str'>
<class 'str'>
🚫 We can not generally put single quotes inside single quotes string ,double quotes inside double quotes string.e.g.
- print('I'm a string') → SyntaxError: invalid syntax
- print("I'm a string") → This is a valid string
🎭 Escape Sequences
- Escape sequences allow you to insert special characters in a string.
- They start with a backslash \. e.g.
- \n → new line
- \t → tab space
- \" → "
- \' → '
- \\ → \
- \ → line join
# Moves World to next line
print("Hello\nWorld")
# Adds a tab space
print("Python\tRocks")
# Prints apostrophe correctly
print("It\'s raining")
# Prints path with backslashes
print("C:\\Users\\Admin")
🖨️ Different Way to print Data
name = "Sadhu"
place = "Kashi"
# 1.Print string data, syntax:
# print("message")
print("Hello World")
print("Hello", "World")
# 2.Print variable data, syntax:
# print(variable)
# print(variable1, variable2)
print(name)
print(name, place)
# 3.Print string and variable data, syntax:
# print("message", variable)
print("Hello " , name , " from " , place)
# 4.Using Format String str.format():
# print("message {}".format(variable))
print("Hello {} from {}".format(name, place))
# 5.Using f-string :
# print(f"message {variable}")
# 6.Printing Without Newline, syntax:
# print("message", end="")
print("Hello " , name , " from " , place, end="")
Hinglish Tip 🗣: Printing ka matlab sirf text dikhana nahi, balki variables, formatting, aur style ke sath output banani hoti hai. f-Strings sabse easy aur fast tarika hai.By default, print() adds a newline at the end.Using end=" " changes it.
Raw Strings
- A raw string in Python is a special type of string where escape sequences (like \n, \t) are treated as literal characters and not interpreted as special characters.
- Raw strings are created by prefixing a string with r or R before the quotes (e.g., r"Hello\nWorld").
- They are useful for writing regular expressions, file paths, or any text where backslashes () should not be treated as escape characters.
Hinglish Tip 🗣: Raw string ka matlab hai backslash (\) ko special character nahi, balki normal character ki tarah treat karna. Ye file paths ya regex ke liye bohot kaam aata hai.
# Create a raw string
raw_string = r"Hello\nWorld"
# Print the raw string
print(raw_string)
# File path example
path = "C:\Users\Admin\notes.txt" # Regular string, interprets \n as newline
print(path) # Output:
# C:\Users\Admin
# otes.txt
raw_path = r"C:\Users\Admin\notes.txt" # Raw string, treats \ as literal
print(raw_path) # Output: C:\Users\Admin\notes.txt
💡 Quick Practice
- Print Exercises
- String Exercises (1,2,3)
- Create a raw string for a Windows file path (e.g., C:\Data\test.txt) and print it.