Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Today: Dive into Python strings — learn how to create, manipulate, and explore them. "String" is any sequence of characters (letters, numbers, symbols etc.) in quotes. Can be represented in single ☝️ or double quotes ✌️
In [1]: # String in single quotes
'I love Pandas Daily'
Out [1]: 'I love Pandas Daily'
In [2]: # String in double quotes
"I love Pandas Daily"
Out [2]: 'I love Pandas Daily'
↩️ Slight digression - Data in python can be stored in something called a variable. Think of a variable as a name or label for your data — saves you from typing the same stuff over and over again.
In [3]: # Assign string to a variable
stringvariable = "I love Pandas Daily" stringvariable
Out [3]: 'I love Pandas Daily'
🔄 Back to Strings Find number of characters (or length) in string.‼️Note: Spaces (" ") are also counted.
In [4]:
len(stringvariable)
Out [4]: 19
In [5]: # Verify the data type (old readers of PD would know)
type(stringvariable)
Out [5]: str
You can have triple quotes (" " ") too. Mandatory when its a multiline string.
In [6]: # "print" command is often used to display output
multilinestring = """I love Pandas Daily""" print(multilinestring)
Out [6]: I love
Pandas Daily What all we can do with 🎸
In [7]:# Adding
"Hello" + "World"
Out [7]: HelloWorld
As I said, spaces (" ") are considered separately
In [8]:"Hello"
+
" "
+
"World"
Out [8]: Hello World
In [9]:# Uppercase
"hello".upper()
Out [9]:'HELLO'
In [10]:# Lowercase
"Hello".lower()
Out [10]:'hello'
In [11]:
# can be applied to variables too
stringvariable.upper()
Out [11]: 'I LOVE PANDAS DAILY'
🧹 Use 'strip' to cleanup starting or ending white spaces
In [12]:
whitespacestring = "Hello white space "
whitespacestring
Out [12]: 'Hello white space '
In [13]:
whitespacestring.strip()
Out [13]:'Hello white space'
💔 And also break them apart
In [14]:
whitespacestring.split(" ")
Out [14]: ['Hello', 'white', 'space', '']
Character Positions in Strings
String’s characters have positions. First character is numbered as 0 and so on.
In [15]:
# Extract 1st letter "H"
"Hello"[0]
Out [15]: 'H'
In [16]:
# Extract 2nd letter 'e'
"Hello"[1]
Out [16]: 'e'
In [17]:
# Can extract first few together
# End position (3) not included "Hello"[0:3]
Out [17]: Hel'
No number before colon means start from '0'
In [18]:# Works same as above
"Hello"[:3]
Out [18]:'Hel'
In [19]:
# To access the end character
"Hello"[-1]
Out [19]:'o'
No number after colon means till end of string
In [20]:
# Start from 2nd position till end
"Hello"[1:]
Out [20]: 'ello'
🔍 Find any text within a string. Tells the position of first character found.
In [21]:
"zuckerberg@meta.com".find("@meta.com");
Out [21]: 10
‼️-1 means text not present
In [22]:
"zuckerberg@meta.com".find("@gmail.com");
Out [22]: -1
↔️ Can replace any text with ease
In [23]:
# Replace meta with gmail
"zuckerberg@meta.com".replace("meta", "gmail")
Out [23]: 'zuckerberg@gmail.com'
That’s it for today — tomorrow lets dive into Python lists. See you in your inbox! 🐼✨
|
Beginner to Expert in Python in just 5 minutes
Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Today: Let’s dive into Python "Dictionaries", the toolbox for storing data with labels and values. If you are an absolute beginner you can glance through previous issues as well Think of Python Dictionaries like a real world dictionary: you look up a word (key) and find its meaning (value). Data is basically stored as key-value pair. Use when you want to connect a label (like a name) to information (like...
Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Today: Get hands-on with Python lists — master storing and managing of multiple items in one place. If you are an absolute beginner you can glance through previous issues as well A "list" is a datatype that can store multiple values as one variable. Yes you can stuff numbers, strings, anything together 🔍 What does it look like? Reminder: print() is often used to display output In [1]: # List is...
Welcome to the very first issue of Pandas Daily — your 5 minute daily dose of Python. Today: Understand some of Python’s data types with examples and clean output. “Data Types” help the computer understand what you're working with—numbers, text, or something else. Common Data Types 1. Numbers Reminder: As seen below, "In [1]" is the code input we will type in and "Out [1]" is the output of code once executed In [1]: # Integers (anything starting with '#' is a comment and not executed) 8 Out...