Swift Program to convert int type variable to string


This tutorial will discuss how to write swift program to convert int type variable to string.

Swift support various datatypes and string and integers are one of them. String is an ordered collection of characters. For example: “TutorialsPoint”, “Pinky”, etc. To create a string-type variable we use String keyword. Whereas Integers represent numeric values like 2, 3, 45, 6, etc. And to create integer-type variables we use the Int keyword.

To convert int type variable into string we uses String() function. This function convert any data type into string.

Syntax

Following is the syntax −

String(variableName)

Below is a demonstration of the same −

Input

Suppose our given input is −

Num = 52324

Output

The desired output would be −

String = “52324”

Example

The following program shows how to convert int type variable to string.

import Foundation
import Glibc

// Integer type variable 
var mynum : Int = 23243

// Convert integer into string 
// Using String() function
var myVar = String(mynum)

print("String:", myVar)
print("Type of myVar variable:", type(of: myVar))

Output

String: 23243
Type of myVar variable: String

Updated on: 13-Dec-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements