String literals in each language: single quotes, double quotes, interpolation, escape sequences (Java, JavaScript, Kotlin, PHP, Python, Ruby)

Overview

--For each programming language (Java, JavaScript, Kotlin, PHP, Python, Ruby), investigate variable expansion and escape sequences when defining a character string enclosed in single quotes (single quotation marks) or double quotes (double quotation marks).

Glossary

Single quotes and double quotes

Generally, in each programming language, when expressing a string literal, it is often the specification that the character string is enclosed in single quotes or double quotes.

--Single quote: "'" --Double quotes: "" "

A single quote is sometimes called a single quotation, and a double quote is sometimes called a double quotation.

Variable expansion

A function that allows the value stored in a variable to be expanded to that location by embedding the variable name in the character string using a predetermined description method.

Escape sequence

A description method for embedding control characters such as line breaks in a character string.

[Escape Sequence -Wikipedia](https://ja.wikipedia.org/wiki/%E3%82%A8%E3%82%B9%E3%82%B1%E3%83%BC%E3%83%97 % E3% 82% B7% E3% 83% BC% E3% 82% B1% E3% 83% B3% E3% 82% B9)

An escape sequence is a sequence of special characters or functions that cannot be represented by a normal character string in a computer system.

Control character -Wikipedia

In computing and telecommunications, control characters (English: control characters) are characters defined in the character code standard that are special operations for displays, printers, communication devices, etc. ( It is a character used to control). It is also called a control character, and is called a "control function character" in the information processing terminology standard.

Specifications of each programming language

Java

--Strings cannot be enclosed in single quotes. If it's just one character, you can enclose it in single quotes, but it's char instead of String --Strings enclosed in double quotes do not expand variables but can be escaped --Variable expansion is possible in a character string with String.format etc.

String (Java SE 11 & JDK 11 )

The String class represents a string. All literal strings, such as "abc", in your Java program will run as instances of this class.

3.10.5. String Literals: Chapter 3. Lexical Structure

A string literal consists of zero or more characters enclosed in double quotes. Characters may be represented by escape sequences (§3.10.6) - one escape sequence for characters in the range U+0000 to U+FFFF, two escape sequences for the UTF-16 surrogate code units of characters in the range U+010000 to U+10FFFF.

3.10.4. Character Literals: Chapter 3. Lexical Structure

A character literal is expressed as a character or an escape sequence (§3.10.6), enclosed in ASCII single quotes. (The single-quote, or apostrophe, character is \u0027.)

Sample code for operation check.

public class Quote {
  public static void main(String[] args) {
    String foo = "Hello";
    String bar = "world";
    System.out.println("Double quote: %s\n%s");
    System.out.println(String.format("Double quote: %s\n%s", foo, bar));
  }
}

Execution result on macOS Mojave + OpenJDK 11.0.2.

$ javac Quote.java 

$ java Quote
Double quote: %s
%s
Double quote: Hello
world

JavaScript

--Strings enclosed in single quotes are not variable-expanded but can be escaped. --Strings enclosed in double quotes do not expand variables but can be escaped --Variable expansion and escape sequences are possible for strings enclosed in grave accents (back quotes).

Handling Text — Strings in JavaScript -Learn Web Development \ | MDN

In JavaScript, you can use single and double quotation marks as quotation marks around a string.

Template literals (Template strings) - JavaScript | MDN

Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them. They were called "template strings" in prior editions of the ES2015 specification.

Sample code for operation check.

const foo = 'Hello';
const bar = 'world';
console.log('Single quote: ${foo}\n${bar}');
console.log("Double quote: ${foo}\n${bar}");
console.log(`Grave accent: ${foo}\n${bar}`);

Execution result on macOS Mojave + Node.js 12.8.0.

$ node quote.js 
Single quote: ${foo}
${bar}
Double quote: ${foo}
${bar}
Grave accent: Hello
world

Kotlin

--Strings cannot be enclosed in single quotes. If it's just one character, you can enclose it in single quotes, but it's Char instead of String --Variable expansion and escape sequences are possible for strings enclosed in double quotes. --A string enclosed in three double quotes (raw string) can be interpolated, but escape sequences cannot be used.

Basic Types: Numbers, Strings, Arrays - Kotlin Programming Language

Character literals go in single quotes: '1'. Special characters can be escaped using a backslash. The following escape sequences are supported: \t, \b, \n, \r, ', ", \ and \$. To encode any other character, use the Unicode escape sequnce syntax: '\uFF00'.

Basic Types: Numbers, Strings, Arrays - Kotlin Programming Language

Kotlin has two types of string literals: escaped strings that may have escaped characters in them and raw strings that can contain newlines and arbitrary text.

Basic Types: Numbers, Strings, Arrays - Kotlin Programming Language

A raw string is delimited by a triple quote ("""), contains no escaping and can contain newlines and any other characters:

Sample code for operation check.

val foo = "Hello"
val bar = "world"
println("Double quote: ${foo}\n${bar}")
println("""Triple quote: ${foo}\n${bar}""")

Execution result on macOS Mojave + Kotlin 1.3.41.

$ kotlinc -script quote.kts 
Double quote: Hello
world
Triple quote: Hello\nworld

PHP

--The string enclosed in single quotes remains raw --Variable expansion and escape sequences are possible for strings enclosed in double quotes.

PHP: String -Manual

string is a concatenation of characters. In PHP, characters are the same as 1 byte. That is, you can use 256 different characters. This also means that PHP does not natively support Unicode.

PHP: String -Manual

The easiest way to specify a string is to enclose it in quotation marks (character'). To specify quotes as literals, they must be escaped with a backslash (\). To specify a backslash as a literal, double it (\\). All backslashes that appear in other situations are treated as backslashes themselves. In other words, writing familiar escape sequences such as \ r and \ n will not have any special effect and will be output in the format as written.

PHP: String -Manual

If the string is enclosed in double quotes ("), PHP interprets the following escape sequences as special characters.

PHP: String -Manual

If the script is enclosed in double quotes or specified in a here document, the variables in it will be parsed.

Sample code for operation check.

<?php
$foo = 'Hello';
$bar = 'world';
echo 'Single quote: {$foo}\n{$bar}', "\n";
echo "Double quote: {$foo}\n{$bar}", "\n";

Execution result on macOS Mojave + PHP 7.3.7.

$ php quote.php 
Single quote: {$foo}\n{$bar}
Double quote: Hello
world

Python

--Strings enclosed in single quotes can be expanded and escaped with f-string. --Strings enclosed in double quotes can be expanded and escaped with f-string.

Built-in — Python 3 \ .7 \ .4 documentation

Python text data is treated as a str object, or string. A string is an immutable sequence of Unicode code points. There are various ways to write string literals:

· Single quotes:'You can embed "double" quotes' Double quotes: "You can embed'single' quotes". ・ Triple quotes:''''Three single quotes'''', "" "Three double quotes" ""

Triple quote strings can be split into multiple lines. All associated whitespace is included in the string literal. String literals that are part of a simple expression and contain only whitespace are implicitly converted to a single string literal. That is, ("spam" "eggs") == "spam eggs".

2 . Lexical analysis — Python 3 \ .7 \ .4 documentation

Literals of these types are enclosed in the corresponding single or double quotes ('), and can also be enclosed in the corresponding triple single or double quotes. (Usually called a triple-quoted string). Backslash (\) characters can escape characters that have a special meaning, such as line breaks, backslashes themselves, quotes, etc. I will.

2 . Lexical analysis — Python 3 \ .7 \ .4 documentation

A formatted string literal or f-string is a string literal prefixed with'f'or'F'. These strings can contain replacement fields, which are expressions separated by curly braces {}. Formatted string literals are evaluated as expressions at run time, whereas other string literals are always constant and do not change.

Sample code for operation check.

foo = 'Hello'
bar = 'world'
print(f'Single quote: {foo}\n{bar}')
print(f"Double quote: {foo}\n{bar}")
print(f'''Triple single quote: {foo}\n{bar}''')
print(f"""Triple double quote: {foo}\n{bar}""")

Execution result on macOS Mojave + Python 3.7.4.

$ python quote.py
Single quote: Hello
world
Double quote: Hello
world
Triple single quote: Hello
world
Triple double quote: Hello
world

Ruby

--The string enclosed in single quotes remains raw --Variable expansion and escape sequences are possible for strings enclosed in double quotes.

Literal \ (Ruby 2 \ .6 \ .0 )

The string is enclosed in double or single quotes. Backslash notation and expression expansion (discussed below) are enabled for strings enclosed in double quotes. Strings enclosed in single quotes do not interpret the contents of the string except for \\ (backslash itself) and '(single quotes). In a string enclosed in single quotes, the \ at the end of the line is interpreted as \ itself.

Literal \ (Ruby 2 \ .6 \ .0 )

You can embed the contents of an expression (a string of it) in the form # {expression} in a string expression, command string, or regular expression enclosed in double quotes ("). Variables Variables that start with a symbol (\ $, @) can also be expanded in the form #variable name by omitting {}. Unless the character following the character # is {, $, @ It is interpreted as the character #. To explicitly stop expression expansion, put a backslash in front of #.

Sample code for operation check.

foo = 'Hello'
bar = 'world'
puts 'Single quote: #{foo}\n#{bar}'
puts "Double quote: #{foo}\n#{bar}"

Execution result on macOS Mojave + Ruby 2.6.3.

$ ruby quote.rb 
Single quote: #{foo}\n#{bar}
Double quote: Hello
world

Recommended Posts

String literals in each language: single quotes, double quotes, interpolation, escape sequences (Java, JavaScript, Kotlin, PHP, Python, Ruby)
Let's write Python, Ruby, PHP, Java, JavaScript side respectively
How to handle JSON in Ruby, Python, JavaScript, PHP
Hello World in various languages [Python / PHP / Java / Perl / Ruby]
Java VS PHP VS Python VS Ruby
Transcendental simple and clear! !! Difference between single quotes and double quotes in Python