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

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!

Related

how to echo text containing with double quotes [duplicate]

This question already has answers here:
How can I escape a double quote inside double quotes?
(9 answers)
Difference between single and double quotes in Bash
(7 answers)
Closed 2 years ago.
I need to echo some text. like text "hey"
If i try with code echo "text "hey"" getting output as text hey
So, how to display the double quotes also. Can anyone help me with this.
You can use
echo 'text "hey"'
or
echo "text \"hey\""
In short:
The double quote ( "quote" ) protects everything enclosed between two double quote marks except $, ', " and \. Use the double quotes when you want only variables and command substitution
Variable - Yes
Wildcards - No
Command substitution - yes
The single quote ( 'quote' ) protects everything enclosed between two single quote marks. It is used to turn off the special meaning of all characters.
Variable - No
Wildcards - No
Command substitution - No
Further details: https://bash.cyberciti.biz/guide/Quoting

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

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

How to get last value before a colon in bash [duplicate]

This question already has answers here:
How to get a substring after the last underscore (_) in unix shell script
(3 answers)
Closed 3 years ago.
I have a string like:
arn:aws:ecs:us-east-1:123456789:task-definition/myservice:10
Is there anyway I can get the last value 10? I tried to get last character but forgot that this int value can increase and eventually becomes 2 characters.
Well, many ways, this one works, though is not elegant :)
echo "arn:aws:ecs:us-east-1:123456789:task-definition/myservice:10" | sed 's/.*://'

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"

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