String Operations
Python tutorial · PySpark.in
String Operations in Python
Python provides many powerful operations to work with strings.
Here are the most important string operations every beginner should learn.
1. String Concatenation (Joining Strings)
Concatenation means joining two or more strings using the + operator.
Example
2. String Repetition
You can repeat a string multiple times using the * operator.
Example
3. String Comparison
Strings can be compared using operators:
- == (equal)
- != (not equal)
- < (less than)
- > (greater than)
- <=
- >=
Python compares strings lexicographically (dictionary/Unicode order).
Example
Case Sensitivity
String comparison is case-sensitive.
4. Escape Sequences in Python
Escape sequences are special characters beginning with \ that allow you to insert:
- new lines
- tabs
- quotes
- backslashes
- Unicode characters
They help format and control how text is displayed.
Common Escape Sequences
Escape | Meaning | Example | Output |
|---|---|---|---|
\' | Single quote | 'It\'s Python' | It's Python |
\" | Double quote | "He said, \"Hello\"" | He said, "Hello" |
\\ | Backslash | "C:\\Users\\Megha" | C:\Users\Megha |
\n | New line | "Hello\nWorld" | Hello |
\t | Tab space | "Hello\tWorld" | Hello World |
\r | Carriage return | "Hello\rWorld" | World |
\b | Backspace | "Hello\bWorld" | HellWorld |
\f | Form feed | Rarely used | Prints on next page |
\ooo | Octal value | "\110\145\154\154\157" | Hello |
\xhh | Hex value | "\x48\x65\x6c\x6c\x6f" | Hello |
\uXXXX | Unicode (16-bit) | "\u2764" | |
\UXXXXXXXX | Unicode (32-bit) | "\U0001F600" | 😀 |
Examples
5. Raw Strings
Raw strings ignore escape sequences.
Prefix the string with r or R.
Example
More Python tutorials
- What is Python and Why is it used for Data Science and Data Engineering?
- How Does Python Work in the Backend? Internal Working of Python
- Top 30 Python Interview Questions for Data Science
- 3-Month Python Roadmap to Excel in Data Science and Machine Learning
- Python Data Types Explained – A Beginner’s Guide
- test
All tutorials · Try the free PySpark compiler · Practice challenges