If input() returns a string, why doesn't print() display the quotation marks? - python-3.x

I'm having trouble understanding the following:
According to my book, unless otherwise specified, input will return a string type. If a string is printed wouldn't you expect the quotes to be included in the result? Is this just how print() is designed to work, if so why?
Example problem:
x = input() # user enters 5.5
print(x) # i expect '5.5' to be printed, instead 5.5 is printed
Wouldn't it be better to print the variable x for exactly what it is?

No, you use quotes to create a literal string; quotes are not part of the string value itself. If you want to see the quotes, ask Python for the representation of your string, i.e.
print(repr(x))

Related

Difference between single quote and double quote string *types* in octave? Reason of warning?

I am aware that in octave escape sequences are treated differently in single/double quotes. Nevertheless, there seems to be a type difference:
Whereas class("bla") and class('bla') are both char,
typeinfo("bla") is string, whereas typeinfo('bla') is sq_string,
which may be short for single quote string.
More interesting, warning("on", "Octave:mixed-string-concat") activates warning
that these two types are mixed.
So after activation, ["bla" 'bla'] yields a warning.
Note that typeinfo(["bla" "bla"]) is string,
whereas if one of the two strings concatenated is single quote, so is the result,
e.g. typeinfo(['bla' "bla"]) is sq_string.
I have a situation where someone activates the warning
and so I want to program so to avoid these.
Thus my question: is there a way to convert sq_string to string?
The core of my problem is that fieldnames seem to be single quoted strings.
What an interesting question. I've never thought one might have a need for such a warning or conversion ... though now that I think about it, it makes sense if you want to collect 'raw' strings, and have their escape sequences interpreted and vice versa ...
After some experimentation, I have found a way to do what you want: use sprintf. This seems to return a (double-quoted) string if your formatted string is in double quotes, and an sq_string if it's in single quotes. If your formatted string is simply "%s", then you can pass a bunch of strings as subsequent arguments, and these will be concatenated (as a double-quoted string).
If you'd prefer to go in the reverse direction and ensure your strings are always single quoted, you can then still do the above with a single-quoted formatted string, or you can just use strcat: this does not trigger your warning, can also be called with a single argument, and seems to always return an sq_string.
Also, since I would generally recommend using either of these with "cell-generated sequence" syntax for convenience, this means that you would be better off "collecting" individual strings in cells more generally. E.g.
a = { 'one', 'two', 'three' }
b = { "four", "five", "six" }
typeinfo( sprintf( "%s", a{:} ) ) % outputs: string
typeinfo( strcat( b{:} ) ) % outputs: sq_string

Does a python string include the quotation marks?

For ('bobby'), is the string here 'bobby' or just bobby? I've tried to research into it but the other questions people ask are more complicated. I only want to know whether a full python string includes or doesn't include the '' marks.
If you are declaring a string, you need the quotation marks, like this example:
a = "Hello"
However, if you are just talking about the string itself, the quotations are not part of it. If I were to print variable a that I declared above, the output would be Hello, not "Hello".
print(a) -> Hello
A string is enclosed within the quotation mark, it does not mean that quotations are included in the string. The quotations are given just to tell the compiler that it is a string data type.
Ex -> "Hello"
'Hello'
But if you include double or single quotes inside single or double quotes in python respectively, then the inner quotation will be treated as a string.
Ex -> 'Ram said, "I love apples."'
"Ram said, 'I love apples.'"

How can i remove (') from a string result, in order to use in subproduct i need not to have (')

Im creating a name based on different inputs by the user, it constructs this way:
Result = str('Blue' + shoetype.result + '5')
Result = 'Bluesandal5'
now i need to use 'Bluesandal5' in another operation, but i need it with out the ('), just Bluesandal5
how can I achieve this?
The ' characters are there for the specific purpose of telling you that it's a string. They disappear when you actually use the string for something. Example, using my python console:
>>> Result = "Bluesandal5"
>>> Result
'Bluesandal5'
>>> print(Result)
Bluesandal5
As you can see, the quotes disappear when it's used in a print() statement. This also holds true for any other operation (e.g. string slicing) - they don't count as part of the string itself.

switching between different quotation marks for output

how can i get my code to change the quotation marks of my output. i saw some references that mentioned json, but i think i need to write it myself.
so i'll post the question and then my code:
Program: quote_me() Function
quote_me takes a string argument and returns a string that will display surrounded with added double quotes if printed
check if passed string starts with a double quote ("\""), then surround string with single quotations
if the passed string starts with single quote, or if doesn't start with a quotation mark, then surround with double quotations
Test the function code passing string input as the argument to quote_me()
[ ] create and test quote_me()
def quote_me (word):
if word == ("\'"):
str(word).replace ("\'", '\"')
else:
return word
print (quote_me(input ("what is the sentence: ")))
maybe i've misunderstood what is required as well, if that's the case, please do tell.
def quote_me(word):
if word.startswith("\"") and word.endswith("\""):
word = word.replace("\"","\'")
elif word.startswith("\'") and word.endswith("\'"):
word = word.replace("\'","\"")
else:
word = "\""+word+"\""
return word

In Swift how to obtain the "invisible" escape characters in a string variable into another variable

In Swift I can create a String variable such as this:
let s = "Hello\nMy name is Jack!"
And if I use s, the output will be:
Hello
My name is Jack!
(because the \n is a linefeed)
But what if I want to programmatically obtain the raw characters in the s variable? As in if I want to actually do something like:
let sRaw = s.raw
I made the .raw up, but something like this. So that the literal value of sRaw would be:
Hello\nMy name is Jack!
and it would literally print the string, complete with literal "\n"
Thank you!
The newline is the "raw character" contained in the string.
How exactly you formed the string (in this case from a string literal with an escape sequence in source code) is not retained (it is only available in the source code, but not preserved in the resulting program). It would look exactly the same if you read it from a file, a database, the concatenation of multiple literals, a multi-line literal, a numeric escape sequence, etc.
If you want to print newline as \n you have to convert it back (by doing text replacement) -- but again, you don't know if the string was really created from such a literal.
You can do this with escaped characters such as \n:
let secondaryString = "really"
let s = "Hello\nMy name is \(secondaryString) Jack!"
let find = Character("\n")
let r = String(s.characters.split(find).joinWithSeparator(["\\","n"]))
print(r) // -> "Hello\nMy name is really Jack!"
However, once the string s is generated the \(secondaryString) has already been interpolated to "really" and there is no trace of it other than the replaced word. I suppose if you already know the interpolated string you could search for it and replace it with "\\(secondaryString)" to get the result you want. Otherwise it's gone.

Resources