4.1.2

Casting

Test yourself

Casting

Casting is the process of converting data from one type to another.

Illustrative background for Reasons for castingIllustrative background for Reasons for casting ?? "content

Reasons for casting

  • One of the most common reasons for casting is output.
  • Output must be formatted as a string, and so we may need to convert a certain piece of data to a string.
  • All input also comes as a string, and must then be converted to other data types.
Illustrative background for Casting functionsIllustrative background for Casting functions ?? "content

Casting functions

  • Casting to a string can be done using the str function. E.g:
    • str(3) gives "3".
  • Casting to an integer can be done using the int function. E.g:
    • int(3.4) gives 3.
  • Casting to a float can be done using the float function. E.g:
    • float("3.4") gives 3.4.

Jump to other topics

1Components of a Computer

2Software & Software Development

3Exchanging Data

4Data Types, Data Structures & Algorithms

5Legal, Moral, Cultural & Ethical Issues

6Elements of Computational Thinking

6.1Thinking Abstractly

6.2Thinking Ahead

6.3Thinking Procedurally

6.4Thinking Logically

6.5Thinking Concurrently

7Problem Solving & Programming

8Algorithms

Unlock your full potential with Seneca Premium

  • Unlimited access to 10,000+ open-ended exam questions

  • Mini-mock exams based on your study history

  • Unlock 800+ premium courses & e-books

Get started with Seneca Premium