0
0
Rubyprogramming~5 mins

String creation (single and double quotes) in Ruby

Choose your learning style9 modes available
Introduction

Strings are used to store words or sentences. You can create them using single or double quotes.

When you want to store a name or message in your program.
When you need to show text on the screen.
When you want to combine words or sentences.
When you want to include special characters like new lines or tabs.
When you want to add variables inside text easily.
Syntax
Ruby
single_quoted_string = 'Hello'
double_quoted_string = "Hello"

Single quotes create simple strings without special processing.

Double quotes allow special characters and variable insertion.

Examples
This creates a string with single quotes and prints it.
Ruby
name = 'Alice'
puts name
This creates a string with double quotes and prints it.
Ruby
greeting = "Hello, world!"
puts greeting
Double quotes let you insert variables inside the string using #{ }.
Ruby
age = 10
puts "I am #{age} years old."
Use backslash to include a single quote inside a single-quoted string.
Ruby
puts 'It\'s sunny today.'
Sample Program

This program shows how to create strings with single and double quotes, insert a variable, and use a newline.

Ruby
name = 'Bob'
greeting = "Hello, #{name}!"
puts greeting
puts 'This is a single-quoted string.'
puts "This is a double-quoted string with a newline\nright here."
OutputSuccess
Important Notes

Single-quoted strings do not process escape sequences like \n (new line).

Double-quoted strings can include special characters like new lines and tabs.

Use #{variable} inside double quotes to add variable values directly.

Summary

Use single quotes for simple text without special characters.

Use double quotes when you need special characters or variable insertion.

Remember to escape quotes inside strings if needed.