Swift – Print Multiple Strings with a Specific Separator

To print multiple strings to console with a specific separator in Swift, use print() function, pass the strings as arguments, and specify the separator string via separator parameter. The separator string is placed between the strings while printing to console.

Examples

In the following example, we print three strings to console, with a separator string " - ".

main.swift

print("apple", "banana", "cherry", separator: " - ")

Output

apple - banana - cherry
Program ended with exit code: 0

Now, let us use comma as a separator string.

main.swift

print("apple", "banana", "cherry", separator: ",")

Output

apple,banana,cherry
Program ended with exit code: 0

The default value for separator parameter is a single space separator=" ". So, if we do not provide a string value for separator, then a space is used as separator while printing the strings.

main.swift

print("apple", "banana", "cherry")

Output

apple banana cherry
Program ended with exit code: 0
ADVERTISEMENT

Conclusion

In this Swift Tutorial, we learned how to print multiple strings to console with a specific separator in between them using print() function, with the help of examples.