- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Print a String in Swift Program?
This tutorial will discuss how to write a swift program toprint a string. A string is a collection of characters such as “TutorialsPoint”, “Hello Purnima! How are you?”, etc. Strings can also be represented by Unicode. We can easily create and modifies a strings because they are lightweight and readable, also we can do string interpolation. Strings can be used to insert variable, constants expressions, etc.
Following is the syntax for creating string type variable
Var a : String
Algorithm to print two strings
Step 1 − Define 2 Variables
Step 2 − Enter string into that variable
Step 3 − Perform operations with that string
Step 4 − Print the output
Print a String
The following program shows how to print a string.
import Foundation import Glibc var string1 : String var string2 : String string1 = "Welcome to tutorialspoint" string2 = "How are you tutorialspoint?" print("String 1: ", string1) print("String 2: ", string2)
String 1: Welcome to tutorialspoint String 2: How are you tutorialspoint?
Print a multiline String
The following program shows how to print a multiline string. Here multilines are enclosed in three double quotes.
import Foundation import Glibc var MultilineData = """ Hello Friends I m Sona. I like to travel very much. I also love to cook food """ print(MultilineData);
Hello Friends I m Sona. I like to travel very much. I also love to cook food
Print a string
The following program shows how to print a string. Here, we do string interpolation
import Foundation import Glibc var Tname = "Sumu" var Pname = "Momos" print("Hello! \(Tname) likes \(Pname) vary much.")
Hello! Sumu likes Momos vary much.
String interpolation means a new string is created by mixing the constant(e.g.,max), expression, variables(e.g., value), and literals along with their values inside the string literal. To insert the values of constant, variable, expression, and literals in string, we use a backslash(\) followed by the values enclosed in the parentheses.
- Related Articles
- How to Print an Integer in Swift Program?
- Swift Program to Print a Set
- Swift Program to Print a Dictionary
- Swift Program to Print a 2D Array
- Swift Program to Print object of a class
- Swift Program to Print an Array
- Swift program to print pascal’s triangle
- Swift program to print Hello World!
- Swift program to print spiral pattern
- Java Program to Print a String
- Kotlin Program to Print a String
- Swift Program to initialize and print a complex number
- Swift Program to Print Boundary Elements of a Matrix
- Swift Program to Insert a string into another string
- Swift Program to Print the ASCII values