Working with Numbers and Strings
Numbers and text are everywhere in programming. Whether you’re building a calculator, making a game, or just printing a friendly greeting, you’ll use…
Numbers and text are everywhere in programming. Whether you’re building a calculator, making a game, or just printing a friendly greeting, you’ll use numbers and strings all the time. If you’re just starting out, don’t worry—this guide will walk you through the basics of Python numbers and strings step by step. No experience needed!
By the end, you’ll be able to do simple math, work with text, and combine these building blocks in your own Python programs. Let’s get started!
What Are Numbers and Strings in Python?
Before we can use numbers and strings, let’s understand what they are.
Numbers are values you can count or measure. In Python, there are two main types:
- Integers: Whole numbers like
5,-3, or100. - Floats: Numbers with decimals, like
3.14,-0.5, or2.0.
Strings are pieces of text. In Python, a string is just a sequence of characters (letters, numbers, symbols) wrapped in quotes. For example:
"Hello, world!"
"42"
"Python is fun!"
Why are these types important? Almost every program needs to work with numbers (like scores, prices, or ages) and strings (like names, messages, or instructions).
Real-life examples:
- Your age:
25(integer) - The price of a coffee:
3.50(float) - Your name:
"Alex"(string) - A welcome message:
"Welcome to LearnPyFast!"(string)
Understanding Python numbers and strings is one of the most important Python basics you’ll learn.
Using Numbers: Basic Math in Python
Python can do math just like a calculator. Here are the basic operations:
- Addition:
+ - Subtraction:
- - Multiplication:
* - Division:
/
Let’s see some examples:
# Addition
print(2 + 3) # 5
# Subtraction
print(10 - 4) # 6
# Multiplication
print(6 * 7) # 42
# Division
print(8 / 2) # 4.0
Notice that division always gives you a float (a number with a decimal point), even if it looks like a whole number.
Integers vs. Floats
- Integers: No decimal point (
7,-2) - Floats: Have a decimal point (
3.5,0.0)
You can mix them in math:
print(5 + 2.5) # 7.5
Everyday programming tasks:
- Calculating someone’s age:
current_year - birth_year - Adding up prices:
coffee_price + muffin_price - Figuring out the average:
(score1 + score2 + score3) / 3
Python math makes these tasks easy!
Quiz Question 1
Question: Which of the following will print the string 'hellohellohello' in Python?
- A) print('hello' * 3)
- B) print('hello' + 3)
- C) print(3 * 'hello')
- D) print('hello' + 'hello' + 'hello')
Working with Strings: Handling Text
Strings are how Python stores and works with text. Let’s look at how to create and use them.
greeting = "Hello, world!"
print(greeting)
You can use either single quotes ('Hello') or double quotes ("Hello"). Just be consistent.
Simple Python String Operations
Here are some handy things you can do with strings:
Joining (Concatenation):
You can join two strings together using the + operator.
first_name = "Ada"
last_name = "Lovelace"
full_name = first_name + " " + last_name
print(full_name) # Ada Lovelace
Repeating:
You can repeat a string using * and a number.
laugh = "ha"
print(laugh * 3) # hahaha
Getting the Length:
Use len() to find out how many characters are in a string.
message = "Python"
print(len(message)) # 6
These Python string operations are useful for handling text in your programs.
Quiz Question 2
Question: What does the len() function do when used with a string in Python?
- A) Adds all the numbers in the string
- B) Returns the number of characters in the string
- C) Changes the string to uppercase
- D) Joins two strings together
Combining Numbers and Strings
Numbers and strings are different types in Python, so they don’t always mix automatically. This can cause surprises if you’re not careful!
Example of a common mistake:
age = 25
print("I am " + age + " years old.")
# This will cause an error!
Python can’t add a string and a number directly. But you can convert numbers to strings with str():
age = 25
print("I am " + str(age) + " years old.") # I am 25 years old.
Or, you can use f-strings (a modern and easy way):
age = 25
print(f"I am {age} years old.") # I am 25 years old.
Converting between numbers and strings:
- To turn a number into a string:
str(42)→"42" - To turn a string into a number:
int("42")→42 - For decimals:
float("3.14")→3.14
This is super useful when you want to display numbers with text or get numbers from user input.
Quiz Question 3
Question: What is the result of the following code?
print("I am " + str(10) + " years old.")
- A) I am 10 years old.
- B) I am str(10) years old.
- C) Error: can't add string and number
- D) I am years old.
Practice: Try It Yourself
The best way to learn Python numbers and strings is by practicing! Here are a few simple tasks. Open your Python editor and give them a try:
- Add two numbers and print the result.
- Create a string with your name and print a greeting.
- Join two strings together (like a first and last name).
- Repeat a string three times and print it.
- Convert a string number (like
"100") to an integer and add 50. Print the result.
Tips for checking your work:
- If you see an error, read the message carefully—it often tells you what went wrong.
- Try changing the numbers or strings and see what happens.
- Don’t worry about making mistakes. Every programmer does!
Keep practicing. The more you play with these basics, the more confident you’ll get.
Quick Review Quiz
Test your understanding with these quick questions:
Quiz Question 4
Question: What’s the difference between an integer and a float in Python?
- A) Integers have decimal points, floats do not
- B) Integers are for text, floats are for numbers
- C) Integers have no decimal point, floats do
- D) There is no difference
Quiz Question 5
Question: How do you convert the string "123" into the number 123 in Python?
- A) str("123")
- B) int("123")
- C) float(123)
- D) "123" + 123
Quiz Answer Key
Question 1
Correct answer: A) print('hello' * 3)
Explanation: Multiplying a string by a number repeats the string that many times. 'hello' * 3 gives 'hellohellohello'.
Question 2
Correct answer: B) Returns the number of characters in the string
Explanation: The len() function counts and returns how many characters are in a string.
Question 3
Correct answer: A) I am 10 years old.
Explanation: str(10) converts the number 10 to the string '10', so the strings can be joined together and printed.
Question 4
Correct answer: C) Integers have no decimal point, floats do
Explanation: Integers are whole numbers without decimals; floats are numbers that can have decimal points.
Question 5
Correct answer: B) int("123")
Explanation: The int() function converts the string "123" into the integer 123.
What’s Next?
Congratulations! You’ve taken a big step by learning the basics of Python numbers and strings. These skills are the foundation for everything else in programming.
Next, you’ll learn about other important data types, like lists (collections of items) and how to get user input. Keep practicing these basics, and soon you’ll be building your own projects.
Remember, every expert programmer started right where you are now. Keep going—you’ve got this!
Ready for more? Check out our next tutorial on lists and user input, and keep building your Python skills with LearnPyFast.com!