Are there any "quote" symbols that you can use if you run out of them?
I know one can use " and ' (and, perhaps a combination of them) like "''" (somehow)
A example of a "additional" "quote" symbol would be
\" inside quotes.
If I make a python script, and "used up" both " and ', is there any more chars that I can use to indicate a quote?
No. From what I can tell, ' and " are the only quotes than can be used in a string literal. The Lexical Analysis page contains this information:
shortstring ::= "'" shortstringitem* "'" | '"' shortstringitem* '"'
longstring ::= "'''" longstringitem* "'''" | '"""' longstringitem* '"""'
In plain English: Both types of literals can be enclosed in matching single quotes (') or double quotes ("). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple-quoted strings).
Which suggests that those are the only two options.
No, " and ' are the only quote characters in Python.
If you run out of quote symbols you can "escape" quotes with a backslash(\), which will ignore the quotes and just make them part of the string.
For example, this would be a valid string:
print("hello \"bob\"")
This would return 'hello "bob"'
No, their are no other quote chracters in Python for right now
As far as I know ' and " are tho only quote characters in python
but if you count a multi line string then it would look like this:
multiline = """
string
string
string
"""
and to make a list of them you would use:
multiline.splitlines()
Related
I need to put the following text format into a variable
"Sometext":"more text";'still text
However, because the text has double and single quotes, I can't put it in a string. I've tried using #''#, and #""# but it's not working.
Sidenote: I can't edit the text because it's originated automatically
What can I do?
Thank you in advance
If it's a literal, you can use a here string:
$variable = #"
"Sometext":"more text";'still text
"#
(Note that the final "# has to be on a separate line, at the very beginning of that line.)
To build strings with complex quotations, consider composite formatting. Save the quotes in a variable and use formatting placeholders to insert them. Like so,
$squote = "'"
$dquote = '"
$myString = "{0}Sometext{0}:{0}more text{0};{1}still text" -f $dquote, $squote
$myString
"Sometext":"more text";'still text
Confused over the purpose of "r". As i understand it helps to read as a normal character than its usage as an escape character
I tried multiple codes as follows and all are giving the same output. This is making me confused on the real interpretation of "r". While i agree with first 3 lines of code.Fourth one is where im confused.
1.re.sub("n\'t", " not", " i am n't happy")
2.re.sub("n\'t", " not", " i am n\'t happy")
3.re.sub(r"n\'t", " not", " i am n\'t happy")
4.re.sub(r"n\'t", " not", " i am n't happy")
Result of all 4 above is :'
' i am not happy'
import re
re.sub(r"n\'t", " not", " i am n't happy")
Given that i have used "r" i expected the backslash to be treated as a characters and not escape character
Actual Output
' i am not happy'
Expected Output
' i am n't happy'
The thing is that there are two layers of -escaping: in the string literal, and in the regex. And in neither does \' have a special meaning, and it's just treated as '.
What using r"" does here is to skip the first string-literal escaping, so that a literal \ is included in the string, but then the regex sees the string \' and just treats it as '.
So all four come down to replacing n't with not.
You still need double backslashes to match a literal backslash.
I am unsure why this doesn't work:
string.replaceAll('\\"','"')
I want to replace all \" with "
Any idea?
I have also tried
string.replaceAll("[\"]","\"")
The first argument to the replaceAll method is a regular expression, so the backslash character has significance there and needs to be escaped. You could use the forward-slash string delimiter to avoid double-escaping.
assert (/Hello, \"Joe\"/.replaceAll(/\\"/, '"') == 'Hello, "Joe"')
I have an assignment to create a lexical analyser and I've got everything working except for one bit.
I need to create a string that will accept a new line, and the string is delimited by double quotes.
The string accepts any number, letter, some specified punctuation, backslashes and double quotes within the delimiters.
I can't seem to figure out how to escape a new line character.
Is there a certain way of escaping characters like new line and tab?
Here's some of my code that might help
< STRING : ( < QUOTE> (< QUOTE > | < BACKSLASH > | < ID > | < NUM > | " " )* <QUOTE>) >
< #QUOTE : "\"" >
< #BACKSLASH : "\\" >
So my string should allow for a quote, then any of the following characters like a backslash, a whitespace, a number etc, and then followed by another quote.
The newline char like "\n" is what's not working.
Thanks in advance!
For string literals, JavaCC borrows the syntax of Java. So, a single-character literal comprising a carriage return is escaped as "\r", and a single-character literal comprising a line feed is escaped as "\n".
However, the processed string value is just a single character; it is not the escape itself. So, suppose you define a token for line feed:
< LF : "\n" >
A match of the token <LF> will be a single line-feed character. When substituting the token in the definition of another token, the single character is effectively substituted. So, suppose you have the higher-level definition:
< STRING : "\"" ( <LF> ) "\"" >
A match of the token <STRING> will be three characters: a quotation mark, followed by a line feed, followed by a quotation mark. What you seem to want instead is for the escape sequence to be recognized:
< STRING : "\"" ( "\\n" ) "\"" >
Now a match of the token <STRING> will be four characters: a quotation mark, followed by an escape sequence representing a line feed, followed by a quotation mark.
In your current definition, I see that other often-escaped metacharacters like quotation mark and backslash are also being recognized literally, rather than as escape sequences.
Is there an easy way, using a subroutine maybe, to print a string in Perl without escaping every special character?
This is what I want to do:
print DELIMITER <I don't care what is here> DELIMITER
So obviously it will great if I can put a string as a delimiter instead of special characters.
perldoc perlop, under "Quote and Quote-like Operators", contains everything you need.
While we usually think of quotes as literal values, in Perl they function as operators, providing various kinds of interpolating and pattern matching
capabilities. Perl provides customary quote characters for these behaviors, but also provides a way for you to choose your quote character for any of
them. In the following table, a "{}" represents any pair of delimiters you choose.
Customary Generic Meaning Interpolates
'' q{} Literal no
"" qq{} Literal yes
`` qx{} Command yes*
qw{} Word list no
// m{} Pattern match yes*
qr{} Pattern yes*
s{}{} Substitution yes*
tr{}{} Transliteration no (but see below)
<<EOF here-doc yes*
* unless the delimiter is ''.
$str = q(this is a "string");
print $str;
if you mean quotes and apostrophes with 'special characters'
You can use the __DATA__ directive which will treat all of the following lines as a file that can be accessed from the DATA handle:
while (<DATA>) {
print # or do something else with the lines
}
__DATA__
#!/usr/bin/perl -w
use Some::Module;
....
or you can use a heredoc:
my $string = <<'END'; #single quotes prevent any interpolation
#!/usr/bin/perl -b
use Some::Module;
....
END
The printing is not doing special things to the escapes, double quoted strings are doing it. You may want to try single quoted strings:
print 'this is \n', "\n";
In a single quoted string the only characters that must be escaped are single quotes and a backslash that occurs immediately before the end of the string (i.e. 'foo\\').
It is important to note that interpolation does not work with single quoted strings, so
print 'foo is $foo', "\n";
Will not print the contents of $foo.
You can pretty much use any character you want with q or qq. For example:
#!/usr/bin/perl
use utf8;
use strict; use warnings;
print q∞This is a test∞;
print qq☼\nThis is another test\n☼;
print q»But, what is the point?»;
print qq\nYou are just making life hard on yourself!\n;
print qq¿That last one is tricky\n¿;
You cannot use qq DELIMITER foo DELIMITER. However, you could use heredocs for a similar effect:
print <<DELIMITER
...
DELIMETER
;
or
print <<'DELIMETER'
...
DELIMETER
;
but your source code would be really ugly.
If you want to print a string literally and you have Perl 5.10 or later then
say 'This is a string with "quotes"' ;
will print the string with a newline.. The importaning thing is to use single quotes ' ' rather than double ones " "