Professional Documents
Culture Documents
Strings 💪
Get ready to dive into the exciting world of Python strings. We'll explore
everything from basic operations to advanced methods. Let's get started!
by Anik Acharjee
String Literals in Python
Single and Double Triple Quotes Escape Characters
Quotes
Triple quotes are used for Escape characters are used to
Python strings can be multiline strings and represent special characters.
represented using single or docstrings. For example: For example, \n represents a
double quotes. For example: """Python is a powerful new line and \\ represents a
'Hello, World!' or "Goodbye, programming language. It is backslash.
Universe!" widely used for data analysis
and machine learning."""
Basic Operations with Strings
Repetition
1 2 3
Concatenation Length
Use the + operator to concatenate two or more Use the len() function to get the length of a string.
strings. For example, "Hello" + "World" gives For example, len("Hello, World!") gives 13.
"HelloWorld".
String Slices and Indexing
You can access individual You can extract a portion of a You can include a step argument
characters in a string using string using slicing. For example, in slicing to skip characters. For
indexing. For example, "Hello"[1] "Hello, World!"[7:12] gives example, "Python is
gives "e". "World". awesome"[::2] gives "Pto sae".
Common String Methods
split()
1 2 3
strip() join()
Returns a copy of the string with leading and Joins the elements of a list into a string using a
trailing whitespace removed. specified separator.
String Formatting in Python
f-Strings str.format()
f-strings are a convenient way to format strings in The str.format() method can be used to format
Python. For example, name = "John" and age = strings. For example, "My name is {} and I am {}
30, f"My name is {name} and I am {age} years years old".format("John", 30) gives "My name is
old" gives "My name is John and I am 30 years John and I am 30 years old".
old".
Regular Expressions for Pattern Matching
in Strings