Skip to ContentGo to accessibility page

2.3 Mixed data types

Learning objectives

By the end of this section you should be able to

  • Identify the data types produced by operations with integers, floats, and strings.
  • Use operators and type conversions to combine integers, floats, and strings.

Combining integers and floats

Programmers often need to combine numbers of different data types. Ex: A program computes the total for an online shopping order:

    quantity = int(input())
    price = float(input())
    total = quantity * price
    print(total)

quantity is an integer, and price is a float. So what is the data type of total? For input 3 and 5.0, total is a float, and the program prints 15.0.

Combining an integer and a float produces a float. A float is by default printed with at least one figure after the decimal point and has as many figures as needed to represent the value. Note: Division using the / operator always produces a float.

Checkpoint

Operations combining integers and floats

Operations combining integers and floats; ch 2, video 5

Concepts in Practice

Operations combining integers and floats

1.
8 * 0.25
  1. 2
  2. 2.0
2.
2 * 9
  1. 18
  2. 18.0
3.
20 / 2
  1. 10
  2. 10.0
4.
7 / 2
  1. 3.0
  2. 3.5
5.
12.0 / 4
  1. 3
  2. 3.0
6.
8 - 1.0
  1. 7.0
  2. 7
7.
5 - 0.25
  1. 4.5
  2. 4.75

Combining numeric types and strings

Easy type conversion in Python can lead a programmer to assume that any data type can be combined with another. Ex: Noor's program reads in a number from input and uses the number in a calculation. This results in an error in the program because the input() function by default stores the number as a string. Strings and numeric data types are incompatible for addition, subtraction, and division. One of the operands needs to be explicitly converted depending on the goal of arithmetic or string concatenation.

The * operator also serves as the repetition operator, which accepts a string operand and an integer operand and repeats the string. Ex: "banjo" * 3 produces "banjobanjobanjo".

Checkpoint

Adding a string and an integer

Adding a string and an integer; ch 2, video 6

Concepts in Practice

Operations combining numeric types and strings

8.
int('34') + 56
  1. '3456'
  2. 90
  3. '90'
9.
str(12) + ' red roses'
  1. '12 red roses'
  2. '12'
  3. Error
10.
'50' * 3
  1. '150'
  2. 150
  3. '505050'
11.
str(5.2) + 7
  1. 12.2
  2. '12.2'
  3. Error
12.
80.0 + int('100')
  1. 180
  2. 180.0
  3. '180'
13.
str(3.14) + '159'
  1. 162.14
  2. '3.14159'
  3. Error
14.
2.0 * 'this'
  1. 'this'
  2. 'thisthis'
  3. Error

Try It

After the point

Write a program that reads in a string of digits that represents the digits after the decimal point of a number, num. Concatenate the input string together with '.' and num, and print the result. Ex: If input is 345, the program will print 2.345.

Try It

Print n times

Write a program that reads in two strings, str1 and str2, and an integer, count. Concatenate the two strings with a space in between and a newline ("\n") at the end. Print the resulting string count times.

Citation/Attribution
Reuse and redistribution of this content in digital or print format:
  • This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's prior written permission.
  • This book uses the Creative Commons Attribution-NonCommercial-ShareAlike License, which means that you can reuse and modify the material only for noncommercial purposes, must attribute OpenStax, and must distribute any derivative works under the same license.
  • Any commercial printing of this textbook, including using a local or custom printer, must be approved by OpenStax, and proper citation provided.
  • OpenStax-copyrighted images, activities, assessments, and similar components of this book are subject to the same licensing – CC-BY-NC-SA. They can be used for noncommercial purposes with attribution. Commercial use requires permission.
  • Permission requests: Anyone who intends to incorporate this content (including text, images, and other components) into large language models, use it in AI offerings, use it commercially (including in print), and/or has questions about another use case is welcome to complete our reuse request form.
Attribution information
  • If you are redistributing all or part of this book in a noncommercial print format, then you must include on every physical page the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

  • If you are redistributing all or part of this book in a noncommercial digital format, then for every page that includes OpenStax content, you must license the derivative work under the same CC-BY-NC-SA license as the original, and include on every digital page view the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

Citation information

The information below includes the information needed to generate citations in most major styles (APA, MLA, etc.); you must reformat and organize the information as needed to fit the requirements of the style. Use the information below to generate a citation. We recommend using a citation tool such as this one.

© Apr 23, 2026 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike License. The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo, and Rice University name, and Rice University logo trademarks, or wordmarks are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.