How to print a string in Golang?


In this tutorial, we will learn how to print a string in the Golang programming language. To print a string in Golang we have to use the package fmt which contains the input/output functions. The correct specifier tells the data type of variable we are printing.

The specifiers we can use to print the strings are:

  • %s − By using this we can print the uninterpreted strings or slice.

  • %q − By using this we can print the string with double quotes.

  • %x − By using this we can print the lower case character string with base 16.

  • %X − By using this we can print the upper case character string with base 16.

Note − If don’t want to use the specifier to print the string directly you can use fmt.Println() function for that.

How to print the string using different specifiers?

Algorithm

  • STEP 1 − Import the fmt package to use the function and print the string inside the fmt package.

  • STEP 2 − First define the string variable and initialize it with some value.

  • STEP 3 − Printing the string using the respective specifiers.

Printing with %s specifier

%s specifier prints the string without any interruption.

Example

package main // fmt package provides the function to print anything import "fmt" func main() { // define and initialize the string variable using var keyword var favSubject string = "Math" // printing string with %s specifier fmt.Println("What is your favorite subject? (define string using var keyword)") fmt.Printf("It's %s\n", favSubject) // define string using the shorthand way favSports := "Cricket" // printing string with %s specifier fmt.Println("What is your favorite Sport? (define string by shorthand way)") fmt.Printf("It's %s\n", favSports) }

In the above code first, we are declaring a variable using the var keyword. Then we are printing using the %s specifier. After that we are declaring a new variable but this time with a shorthand way and printing using %s specifier.

Output

What is your favorite subject? (define string using var keyword)
It's Math
What is your favorite Sport? (define string by shorthand way)
It's Cricket

Printing with %q specifier

%q specifier helps in printing the string with double quotes by ignoring Golang syntax.

Example 2

package main // fmt package provides the function to print anything import "fmt" func main() { // define and initialize the string variable using the var keyword var highestPeak string = "Mount Everest" // printing string with %s specifier fmt.Println("What is your highest peak in the world?") fmt.Printf("It's %q\n", highestPeak) }

In the above code first, we are declaring a variable using the var keyword. Then we are printing using the %s specifier. Notice the output below the word Mount Everest is in double quotes. So if someone wants to print any string with double quotes you can do it with %q specifier.

Output

What is your highest peak in the world?
It's "Mount Everest"

Printing with %x specifier

%x is used to print the lowercase alphabet with base 16.

Example

package main // fmt package provides the function to print anything import "fmt" func main() { // define and initialize the string variable using var keyword var lowerCaseAlphabet string = "abcd" // printing string with %x specifier fmt.Println("Printing lower case alphabet in base 16") fmt.Printf("%x\n", lowerCaseAlphabet) }

In the above code first, we are declaring a variable using the var keyword. Then we are printing using the %x specifier. This specifier will print the lower case alphabet value with base 16.

Output

Printing lower case alphabet in base 16
61626364

Printing with %X specifier

%X is used to print the upper case alphabet with base 16.

Example

package main // fmt package provides the function to print anything import "fmt" func main() { // define and initialize the string variable using var keyword var upperCaseAlphabet string = "ABCD" // printing string with %s specifier fmt.Println("Printing lower case alphabet in base 16") fmt.Printf("%x\n", upperCaseAlphabet) }

In the above code first, we are declaring a variable using the var keyword. Then we are printing using the %X specifier. This specifier will print the upper case alphabet value with base 16.

Output

Printing lower case alphabet in base 16
41424344

These were different to print the string using different specifiers. The use cases are supposed you are working on some task and have to print a string with double quotes included then you can use %q specifier. Similar way you can use other specifiers also. To learn more about go you can explore these tutorials

Updated on: 26-Aug-2022

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements