×

Go Tutorial

Go Basics

Go Variables

Go Literals

Go Type Handling

Go Operators

Go Decision Making

Go Loops

Go Functions

Go String

Go Arrays

Go Slices

Go Maps

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

How to print double-quoted string in Golang?

Last Updated : April 20, 2025

Given a string, we have to print it within double-quotes.

Printing the double-quoted string in Golang

When we want to print a string using the fmt.Printf function – we use "%s" format specifier. But, "%s" prints the string without double-quotes.

To print double-quoted string – we can use "%q" format specifier.

Golang code to print double-quoted string

In this example, we are declaring a string variable str and printing it using "%s" and "%q" format specifier to understand the difference between printing the string with and without double quotes.

// Golang program to print double-quoted string

package main

import (
	"fmt"
)

func main() {
	str := "Hello, world!"

	fmt.Printf("value of str is: %s\n", str)
	fmt.Printf("value of str is: %q\n", str)
}

Output

value of str is: Hello, world!
value of str is: "Hello, world!"

Additional Approaches

1. Using fmt.Println for automatic double quotes

We can also use the fmt.Println function which will automatically print strings with double quotes when we use the %q format specifier.

// Golang program using fmt.Println with %q format specifier

package main

import (
	"fmt"
)

func main() {
	str := "Hello, world!"

	fmt.Println("value of str is: ", str)
	fmt.Println("value of str is: ", fmt.Sprintf("%q", str))
}

2. Using string concatenation

Another approach is to manually concatenate the double quotes with the string before printing.

// Golang program to manually add double quotes

package main

import (
	"fmt"
)

func main() {
	str := "Hello, world!"

	// manually concatenating double quotes
	fmt.Println("value of str is: " + "" + str + "")
}

3. Using fmt.Sprintf for custom formatting

We can also use fmt.Sprintf to format the string with custom quote characters.

// Golang program using fmt.Sprintf for custom quotes

package main

import (
	"fmt"
)

func main() {
	str := "Hello, world!"

	result := fmt.Sprintf(""%s"", str)
	fmt.Println("value of str is: ", result)
}
Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.