Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am learning Python by using Pycharm from Jetbrains . I am unable to pass this Question :
Print out this text using one String "The name of this ice-cream is "Sweet'n'Tasty"
My solution is:
print('The name of this ice-cream is \"Sweet\'n\'Tasty\"')
It shows the right output but the program does not accept is as a solution.
Why the seemingly right answer is not accepted, and how to satisfy given requirement?
I found this page because I ran into the exact same issue, earlier today.
In short, it's the lesson's logic being picky on syntax, in a very unclear way. You're typing the right thing for what it's asking, and you're typing something that parses with no error-- but the lesson rejects it anyway.
The long, drawn out answer.
The task is:
A backslash is used to escape a quote so it can be used in strings such as 'It\'s me' and "She said \"Hello\"". The special symbol '\n' is used to add a line break to a string.
Print out this text using one string: The name of this ice-cream is "Sweeet'n'Tasty"
And they provide to you:
dont_worry = "Don't worry about apostrophes"
print(dont_worry)
print("The name of this ice-cream is \"Sweeet\"")
print('text')
where the text in the print('text') on the last line is meant to be replaced with your answer.
The lesson seems to want to teach you that you can escape both a single quote and a double quote with a backslash. Note the double quotes on the second print instance, and the single quote on the third print instance [where they want you to type your answer]
if you replace the text in print('text') with: The name of this ice-cream is \"Sweeet'n'Tasty\"
the task fails with:
File
"/PycharmProjects/PythonIntroduction/lesson3/task8/character_escaping.py",
line 4
print('The name of this ice-cream is \"Sweeet'n'Tasty\"')
^ SyntaxError: invalid syntax
so if you add slashes to the single quotes [apostrophes in this context]
print('The name of this ice-cream is \"Sweeet\'n\'Tasty\"')
even though the text does parse correctly in the console below:
Don't worry about apostrophes
The name of this ice-cream is "Sweeet"
The name of this ice-cream is "Sweeet'n'Tasty"
The task fails and you get an infuriating error that reads:
Use backslash () to escape quotes
Even though that's what you've done.
If you try to backspace the single quotes to replace with double quotes for print('text') you get:
It's not allowed to delete answer placeholders
Now - if you move cursor [or caret] next to a quote, you get a lightbulb hint that gives you an option to:
Convert single quoted string to double-quoted string
which is what they want you to do ... but through some logic error, it takes your typed answer of
print('The name of this ice-cream is \"Sweeet\'n\'Tasty\"')
and munges it up to:
print("The name of this ice-cream is \\")
and the boundaries for the answer box text get all fouled up and wrap up to the previous line-- it looks like a bug in the lesson software itself.
SO
what you must do from the start is use your cursor to get the lighbulb hint FIRST and "Convert single quoted string to double-quoted string"
so that print('text') becomes print("text")
^^^ note the change from single to double quotes ^^^
and THEN type your correct answer of
print("The name of this ice-cream is \"Sweeet'n'Tasty\"")
This took me MUCH longer than I would have liked it to, to figure it out. As a beginner in programming and brand-new to Python, this was a huge roadblock. If I were using this in an instructor-led course they might have said "Oh, this is a bug in the software, you can see we're getting the right answer, let's skip it and move on." but for self-study, I was convinced I wasn't right, and I was overlooking something basic. It was discouraging as slamming into a brick wall. Repeatedly.
I guess the teachable moment here is:
Sometimes the textbook is wrong and you have to understand & prove it to yourself why.
You can also try using as in python if you use a single quote outside then double quotes are allowed within the String.
`print('The name of this ice-cream is "Sweet\'n\'Tasty"')`
Also for more reference, you can have a look here
I hope that helps.
Related
I've been trying to remove pair parentheses(including text between them), unbalanced parentheses and quotation marks from the string.
What I've done so far:
import re
sample_text = '""sads"add"sfsfdsfds()()(0sefdAAAsfs)dasdad(asd'
res = re.sub(r'\([^)]*\)', '', sample_text))
It matches with only ()()(0sefdAAAsfs) part of the text. Unbalanced and quotation marks left unmatched. What can be done to improve above regex?
This isn't really something that a regular expression is suited for, so is not the right tool for the job. Having said that, you can use the following pattern to see if there is an opening paren, then zero or more non-paren, and a matching closing paren:
\([^)+]*\)
Substitute a " or ' or [ or whatever for the other types of matching components.
But again, this would not work with something like this:
(asdf))))))))
Long story short: it's not a problem that a regular expression is capable of solving. Try testing it out here: https://regex101.com/r/bdiK5W/2.
str = "fa, (captured)[asd] asf, 31"
for word in str:gmatch("\(%a+\)") do
print(word)
end
Hi! I want to capture a word between parentheses.
My Code should print "captured" string.
lua: /home/casey/Desktop/test.lua:3: invalid escape sequence near '\('
And i got this syntax error.
Of course, I can just find position of parentheses and use string.sub function
But I prefer simple code.
Also, brackets gave me a similar error.
The escape character in Lua patterns is %, not \. So use this:
word=str:match("%((%a+)%)")
If you only need one match, there is no need for a gmatch loop.
To capture the string in square brackets, use a similar pattern:
word=str:match("%[(%a+)%]")
If the captured string is not entirely composed of letters, use .- instead of %a+.
lhf's answer likely gives you what you need, but I'd like to mention one more option that I feel is underused and may work for you as well. One issue with using %((%a+)%) is that it doesn't work for nested parentheses: if you apply it to something like "(text(more)text)", you'll get "more" even though you may expect "text(more)text". Note that you can't fix it by asking to match to the first closing parenthesis (%(([^%)]+)%)) as it will give you "text(more".
However, you can use %bxy pattern item, which balances x and y occurrences and will return (text(more)text) in this case (you'd need to use something like (%b()) to capture it). Again, this may be overkill for your case, but useful to keep in mind and may help someone else who comes across this problem.
I did use the manual but I am unable to get all the options together to understand what the above code is actually doing.
awk -v v='"' 'BEGIN{FS=OFS=v}{gsub(",","",$2);print }' \
${SOURCE_LOCATION}/TEMP1_$file_name>${SOURCE_FILE_LOCATION}/TEMP2_$file_name
When do we have to use the curly brackets in a code after the '$' and when not to. Please explain. Any help is really appreciated.
This command would remove all the commas in the second field. The field separator being the quote character " (as specified by FS).
For example, the following string:
something "string, with, commas" something "else, here, and more"
would be transformed to:
something "string with commas" something "else, here, and more"
The significance of {} in variable names has been well explained by #Joni.
The input is read from the file ${SOURCE_LOCATION}/TEMP1_$file_name and output is redirected to ${SOURCE_LOCATION}/TEMP2_$file_name.
You must use the curly brackets syntax when a variable name is followed by something that's not part of the variable name but could be confused with it. For example, compare
hello="Hello"
echo $hello_world
with
hello="Hello"
echo ${hello}_world
The first one outputs an empty line (or the value of the shell variable hello_world, if it exists), and the second one outputs Hello_world.
In your case they are not necessary because a slash can never be a part of the variable name. Some people prefer to use the brackets to make it clear where the variable begins and where it ends even when they are not required.
Was hoping someone could help me with an issue I am having with escape characters in Cucumber/Watir.
I have automated tests setup. When I perform a search, 1 of the assertions I use to verify that the search has returned the correct result is to check the page for text. So my code looks like this:
Then /^I should see the following text: "([^"]*)"$/ do |str|
assert #browser.text.include?(str)
end
Here I pass in the text to search for in the string variable. e.g nike, reebok etc
So in my feature file the step is like this:
Then I should see the following text "search results for nike"
This works fine apart from 1 issue. 1 of the sites I am testing has decided to put the search term in double quotes i.e - search results for "nike"
As a result this screws up my test as I need to include the quotes as part of the search term. Therefore I need to put the word nike in escape quotes or else cucumber will recognise the first quotation around the word nike as a closing quotation. (as there is already a double quotes before it)
I have tried various different escape characters but nothing seems to work. For example I have tried the following:
\" – double quote
\\ – single backslash
Has anyone experienced similar problems and if so, how did you overcome the problem?
Thanks!
You need to change the regex rather than the string.
Problem: Your current regex says "([^"]*)", which says to match all characters between the quotations that are not quotations. This is not good given that you want to include quotations.
Solution: Change the step to the following:
Then /^I should see the following text: "(.*?)"$/ do |str|
assert #browser.text.include?(str)
end
The .* says to match all characters between the quotations. The ? makes the search lazy (instead of greedy). The ? is optional in this case, but would be important if there were additional parameters being captured. A good explanation of the greedy vs lazy can be seen at http://www.regular-expressions.info/repeat.html.
The % key is one of the best features of vim: it lets you jump from { to }, [ to ], and so on.
However, it does not work by default with quotes: Either " or ', probably because the opening and closing quote are the same character, making implementation more difficult.
Thinking a bit more about the problem, I'm convinced that it should be implemented, by counting if the number of preceding quotes is odd or even and jumping to the previous or next quote, accordingly.
Before I try to implement it myself, I'd just like to know if someone already has?
Depending on your reason for needing this, there may be a better way to accomplish what you're looking for. For example, if you have the following code:
foo(bar, "baz quux")
^
and your cursor happens to be at the ^, and you want to replace everything inside the quotes with something else, use ci". This uses the Vim "text objects" to change (c) everything inside (i) the quotes (") and puts you in insert mode like this:
foo(bar, "")
^
Then you can start typing the replacement text. There are many other text objects that are really useful for this kind of shortcut. Learn (and use) one new Vim command per week, and you'll be an expert in no time!
Greg's answer was very useful but i also like the 'f' and 'F' commands that move the cursor forward and backward to the character you press after the command.
So press f" to move to the next " character and F" to move to the previous one.
I have found this technique very useful for going to the start/end of a very long quoted string.
when cursor is inside the string, visually select the whole string using vi" or vi'
go to start/end of the string by pressing o
press escape to exit visual select mode
this actually takes the cursor next to the start/end quote character, but still feels pretty helpful.
Edit
Adding Stefan's excellent comment here which is a better option for anyone who may miss the comment.
If you use va" (and va') then it will actually visually select the quotes itself as well.
– Stefan van den Akker
I'd like to expand on Greg's answer, and introduce the surround.vim plugin.
Suppose that rather than editing the contents of your quotes, you want to modify the " characters themselves. Lets say you want to change from double-quotes to single-quotes.
foo(bar, "baz quux")
^
The surround plugin allows you to change this to
foo(bar, 'baz quux')
^
just by executing the following: cs"' (which reads: "change the surrounding double-quotes to single-quotes").
You could also delete the quote marks simply by running: ds" (which reads: "delete the surrounding double-quotes).
There is a good introduction to the surround plugin here.
I know this question is old but here is a plugin to use % to match the corresponding double quote:
https://github.com/airblade/vim-matchquote