Why doesn't multi line print in python work for single and double quoted strings? [duplicate] - python-3.x

This question already has answers here:
Why are three apostrophes needed for print in Python?
(4 answers)
How do I split the definition of a long string over multiple lines?
(30 answers)
Closed 2 years ago.
This program doesn't work
print("test
test")
This runs as expected
print("""test
test""")
Is this simply because Python interpreter allows multi line strings for triple quotes, or there is some difference in the way it interprets the triple quotes?

When You manually type \n it is interpreted as command to execute, you should use triple quotes to enter newlines(\n) manually. Either """#Your string""" or '''#Your string'''

the triple quotes are also used to define multi line string that why ,
print("""test
test""")
the above code works,
You can also use the \ such as,
print("test \
test")
Have a look at this question-Pythonic way to create a long multi-line string

Related

How to calculate the length of a xterm control sequence [duplicate]

This question already has answers here:
How can I remove the ANSI escape sequences from a string in python
(8 answers)
Closed 3 years ago.
I am looking to format text before passing it to stdout/console. To do this properly I need to know how long a given section of text is after being printed to the console, which requires knowing how much characters will not be printed due to them being escape sequences.
On the web I have found multiple documentations for these sequence commands, but there seems to be no quick and easy way to find out where a given escape sequence command ends unless I was to parse them. Is there a trick/solution to getting the length of any escape sequence command without parsing?
How many characters is this \x1b[38;2;20;60;122m string \x1b[0m in console?
You can use ansifilter:
$ printf '\x1b[38;2;20;60;122mabc\x1b[0m' | ./ansifilter --no-trailing-nl | wc -c
3

Using double quotes inside a single quotes string changes escape behavior [duplicate]

This question already has answers here:
double quotes in string representation
(1 answer)
Understanding difference between Double Quote and Single Quote with __repr__()
(3 answers)
Closed 4 years ago.
Take a look at the following example:
In [1]: u'{u\'cname\': u"A\'B"}'
Out[1]: u'{u\'cname\': u"A\'B"}'
In [2]: u'{u\'cname\': uA\'B}'
Out[2]: u"{u'cname': uA'B}"
As you can see, the first string has additional pair of " in it, and it looks like this change is causing the entire escape logic to be altered - in the second example, there are no backslashed printed, while in the first one there are.
Why does this happen?
Thanks!

Unable To Concatenate 2 Linux Variables Into 1 [duplicate]

This question already has answers here:
How to concatenate string variables in Bash
(30 answers)
Filename not printing correctly with underscore "_" in Bash [duplicate]
(2 answers)
Closed 4 years ago.
I'm trying to concatenate two variables in linux tectia SSH into one variable, separated by "_". For some reason only one of the two variables is printed out.
I've tried to concatenate via " " e.g.:
sample1="$var1_$var2"
or
sample1="$var1 _ $var2"
and I've tried to concatenate directly e.g.:
sample1=$var1_$var2
Would appreciate any help given!
cnt_abr1=ab
cnt_abr2=cd
cnt_abr3=ef
env_abr1=a
env_abr2=b
sample1="$env_abr1_$cnt_abr1"
sample2=$env_abr2_$cnt_abr3
echo $sample1
echo $sample2
Output:
_ ab
ef
Since underscores are effectively letters, bash has no way of knowing when your variable name ends and your literal underscore begins. The proper way to reference the variables is with ${...} in this case, which unambiguously delimits the name from the rest of the command line:
sample1="${env_abr1}_${cnt_abr1}"
sample2=${env_abr2}_${cnt_abr3}
In both cases, the second name does not require special treatment. Any other (semantically valid) non-letter character would do as well, as you pointed out in your comment:
sample1="$env_abr1"_"$cnt_abr1"
sample2="$env_abr2"_"$cnt_abr3"

Why do we need special treatment of multiline string literals in programming languages? [duplicate]

This question already has an answer here:
What's the advantage of having multi-line & single-line string literals in python?
(1 answer)
Closed 8 years ago.
Python has """ for multiline string literals.
C/C++ use \ at the end of a line to split a string literal across multiple lines.
Go uses `` for the same.
Can a language not support multiline string literals only with a single quote character?
Why not have multiline string literals as the default and only supported form?
Python also support \ at the end of a line to split a string literal across multiple lines.

python, \t works different when passed as argument (in Eclipse) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python: Split string with multiple delimiters
Convert django Charfield “\t” to tab
I have written a python code in Eclipse which takes delimiters as an argument. When I do
print "Hello",delimiter, "All".
This generates --> Hello \t All, whereas if I overwrite the delimiter with delimiter = '\t' within the code, I get the right output Hello All. I wonder what is the difference? I hope this not just the eclipse thing.
The problem is that what is being passed in from the command line is actually a string of length two "\\t" and not a tab character. You can do the following to your delimiter
delimiter.decode("string_escape"))
that should convert the string '\\t' into '\t'. The answer comes from a duplicate questions here

Resources