Is there a way I can print a statement with an exclamation mark and a quotation mark? - python-3.9

I'm trying to print a statement that looks like this: "Oh no!",
but I keep getting: Oh no! without the quotes.
This is the code I've been using:
print(exclamation.capitalize() + ("!") , "I yelled" + ".")
Please what am I missing?

In Python 3.6+, you can use f-strings to embed statements directly in the string.
Also, if you use single quotes to capture the string, you can use double-quotes in the string and they will print without needing to be escaped.
print(f'"{exclamation.capitalize()}"! I yelled.')

Related

I want to print a one-liner but I have a problem with printing command varibles, is there like a paramater that ignores them? [duplicate]

Can anyone tell me how I can type a backtick in my shell variable?
I am building a SQL query in a variable.
The column name for that query is also a variable and i need to put it between backticks (it has to be a backtick, not a ' or a ").
example:
SQLQUERY="select `${columnname}` from table"
Thanks!
Use single quotes around the parts containing the back-ticks, or escape the back-ticks with a backslash:
SQLQUERY='select `'"${columnname}"'` from table'
SQLQUERY="select \`${columnname}\` from table"

ruby .split('\n') not splitting on new line

Why does this string not split on each "\n"? (RUBY)
"ADVERTISING [7310]\n\t\tIRS NUMBER:\t\t\t\t061340408\n\t\tSTATE OF INCORPORATION:\t\t\tDE\n\t\tFISCAL YEAR END:\t\t\t0331\n\n\tFILING VALUES:\n\t\tFORM TYPE:\t\t10-Q\n\t\tSEC ACT:\t\t1934 Act\n\t".split('\n')
>> ["ADVERTISING [7310]\n\t\tIRS NUMBER:\t\t\t\t061340408\n\t\tSTATE OF INCORPORATION:\t\t\tDE\n\t\tFISCAL YEAR END:\t\t\t0331\n\n\tFILING VALUES:\n\t\tFORM TYPE:\t\t10-Q\n\t\tSEC ACT:\t\t1934 Act\n\t"]
You need .split("\n"). String interpolation is needed to properly interpret the new line, and double quotes are one way to do that.
In Ruby single quotes around a string means that escape characters are not interpreted. Unlike in C, where single quotes denote a single character. In this case '\n' is actually equivalent to "\\n".
So if you want to split on \n you need to change your code to use double quotes.
.split("\n")
Ruby has the methods String#each_line and String#lines
returns an enum:
http://www.ruby-doc.org/core-1.9.3/String.html#method-i-each_line
returns an array:
http://www.ruby-doc.org/core-2.1.2/String.html#method-i-lines
I didn't test it against your scenario but I bet it will work better than manually choosing the newline chars.
Or a regular expression
.split(/\n/)
You can't use single quotes for this:
"ADVERTISING [7310]\n\t\tIRS NUMBER:\t\t\t\t061340408\n\t\tSTATE OF INCORPORATION:\t\t\tDE\n\t\tFISCAL YEAR END:\t\t\t0331\n\n\tFILING VALUES:\n\t\tFORM TYPE:\t\t10-Q\n\t\tSEC ACT:\t\t1934 Act\n\t".split("\n")

Ruby Quotation Regex

I wanted to ask this as I looked and it's specific and couldn't find other threads on it.
I want to make a regex that will Capture everything that would lie between two quotations and the quotations as well surrounding.
like: "insert whatever string here (which can include " "'s)"
basically I want a regex line that would take the quotations AND everything in between them (can be anything).
So a line with quotations and anything that lies inside of it.
I can't seem to figure this out.
I think you are just having a problem with the single and double quotes. Use this:
%q{like: "insert whatever string here (which can include " "'s)"}[/".*"/]
From the regex side of things, you could try this:
str = %q{uncaptured " captured " " /captured " /uncaptured}
str[/".*"/]
#=> "" captured " " /captured ""
For a non-regex solution, you just find the first and last index and collect the substring in between:
str[str.index('"')..str.rindex('"')]

How do I eval multiple lines of code in ruby?

eval('puts "ff"\nputs "ff"')
I tried to use two expressions in one eval but it doesn't execute?
How do I do this? I want to know because I want to dynamically execute partial code.
With heredoc syntax. File and line number are passed to give reference information in back traces.
eval(<<-CODE, __FILE__, __LINE__ +1 )
some(:ruby);
code
# and comments
CODE
eval("puts 'ff'\nputs 'ff'")
also works. '\n' gets treated as literally a slash and an n, because single quotes work differently to double quotes.
I use this:
eval %{
puts 'ff'
puts 'hello'
}
Do:
eval('puts "ff";puts "ff"')

Watir magic escape sequence?

I am currently using Watir with Firefox and it seems that when I try to set a field with the following text:
##$QWER7890uiop
The command I am using is the following:
text_field(:name, "password").value=("!##$QWER7890uiop)
I've also tried this:
text_field(:name, "password").set "!##$QWER7890uiop)
Only the first 2 characters get entered. Is there something I can do to by pass this feature?
You need to escape the string using single quotes '.
text_field(:name, "password").value='"!##$QWER7890uiop'
Many characters are substituted inside double quotes.
Escape sequences like \n, \t, \s, etc are replaced by their equivalent character(s). See here for full list.
#{} where anything the braces is interpreted as a ruby expression.
#$something where $something is interpreted as a ruby global variable. That's the problem with your quote above, beside not being terminated.
%s is interpreted as an ERB template expression (it is interpolated).
For instance:
puts "%s hours later" % 'Five'
results in
"Five hours later".

Resources