Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

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

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.


Operations combining integers and floats

Concepts in Practice

Operations combining integers and floats

8 * 0.25
  1. 2
  2. 2.0
2 * 9
  1. 18
  2. 18.0
20 / 2
  1. 10
  2. 10.0
7 / 2
  1. 3.0
  2. 3.5
12.0 / 4
  1. 3
  2. 3.0
8 - 1.0
  1. 7.0
  2. 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".


Adding a string and an integer

Concepts in Practice

Operations combining numeric types and strings

int('34') + 56
  1. '3456'
  2. 90
  3. '90'
str(12) + ' red roses'
  1. '12 red roses'
  2. '12'
  3. Error
'50' * 3
  1. '150'
  2. 150
  3. '505050'
str(5.2) + 7
  1. 12.2
  2. '12.2'
  3. Error
80.0 + int('100')
  1. 180
  2. 180.0
  3. '180'
str(3.14) + '159'
  1. 162.14
  2. '3.14159'
  3. Error
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.


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 permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at
Citation information

© Feb 26, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.