Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single vs Double quotes in Julia

What is the difference between single quotes and double quotes in Julia?

Unlike Python, for strings, it doesn't allow single quotes:

> s = 'abc'
syntax: invalid character literal
> s = "abc"
> print(s)
abc

But when trying to single quote a double quote, it's allowed:

> s = '"'
> print(s)
"

What is the single quote use for in Julia? Is there documentation like Python's PEP to explain for reason why single quotes are not used?

like image 692
alvas Avatar asked Feb 07 '17 02:02

alvas


People also ask

What is the difference between single and double quotation marks?

General Usage Rules In America, Canada, Australia and New Zealand, the general rule is that double quotes are used to denote direct speech. Single quotes are used to enclose a quote within a quote, a quote within a headline, or a title within a quote.

Should I use single or double quotes?

In US English, you must use double quotation marks. Single quotation marks are used for quotes within quotes. In UK English, it's most common to use single quotation marks, with double quotation marks for quotes within quotes, although the other way around is acceptable too.

What are single and double quotes?

The main difference between double quotes and single quotes is, double quotes are used to denote a speech or a quotation whereas single quotes are used to indicate quote within a quotation.

How do you know if strings are equal in Julia?

The cmp() is an inbuilt function in julia which is used to return 0 if the both specified strings are having the same length and the character at each index is the same in both strings, return -1 if a is a prefix of b, or if a comes before b in alphabetical order and return 1 if b is a prefix of a, or if b comes before ...


1 Answers

Think of it like in C/C++; a single quote makes a Char, while double quotes make a String (see, e.g., here).

julia> c = 'a'
'a'
julia> typeof(c)
Char
julia> s = "a"
"a"
julia> typeof(s)
String
julia> s = "ab"
"ab"
julia> typeof(s)
String

In Python we just use a string of length one as characters, but Julia distinguishes between them, and so we get

julia> typeof("abc"[1:1])
String    
julia> typeof("abc"[1])
Char

even though in Python we have

>>> type("abc"[0:1])
<type 'str'>
>>> type("abc"[0])
<type 'str'>
like image 178
DSM Avatar answered Sep 20 '22 17:09

DSM