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?
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.
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.
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.
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 ...
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'>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With