Why this happens [closed] - python

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 last year.
Improve this question
my_variable_name = str("John")
print(my_variable_name)
This is the code for example, now if I happen to add the double quotes around "my_variable_name" in this statement:
print(my_variable_name)
It just simply display whatever is written inside the "" but if I don't add it'll print "john". Now what I think the reason is because when compiler find something inside "" that tells it to display whatever datatype it is while without "" it just displays the stored or u can say assigned value . I know its easy (basis) but I do this like all the time and my knowledge about this problem never satisfies me

The problem has nothing to do with print function.
"my_variable_name" is a string literal. Single or triple quotes could also be used.
my_variable_name is a reference to a previously defined variable of that name. The type of the variable's value could be anything.
You can print any object, and it'll return the str() representation of it.
Unrelated, you don't need str() function to define a string literal.
when compiler find something inside "" that tells it to display whatever datatype it is
Python is an interpreted language, it's not the compiler doing this. The datatype of anything enclosed in quotes is always a string

Related

How do I add quotes in a string concatenation operation in Python? [closed]

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 1 year ago.
Improve this question
If on the python shell we put:
name='stackof'
name+'runs'
Shouldn't it give
'stackof''runs'
But it gives
'stackofruns'
No it actually appends both the strings. For the behaviour you want you have to write like this
name="'stackof'"
then
name + "'runs'"
You can get the result that you want if you do the following:
>>>name = "stackof"
>>>name + "''" + "runs"
>>>"stackof''runs"
Even when you assign "runs" to another variable and concatenate the two, it would result 'stackofruns'.
A string represents character sequences within quotes, either single quotes ' ' or double quotes " ".
For the string name = 'stackof' the actual contents are stackof, the quotes are not included.
So when name+'runs' is executed the contents inside the quotes i.e stackof and runs are taken and the result is stored inside a new string as 'stackofruns'
name = "'stackof'"
print(name+"'runs'")

How to use the actual value of a property in node's code? [closed]

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 1 year ago.
Improve this question
I'm trying to use the value of a variable in code itself and I am not sure how to do it using Python for NUKE. I'm using the following code in a NUKE's node's knob that can't refer to the value of variable name. This knob will be executed later when there will be no variable name.
name = "aa124"
I want to create a one liner code that will be stored in nuke Python knobs and will be executed later.
I can print a string:
print("aa124")
But I cannot print my property's value:
print name
Could someone help me?
Based on my understanding, print any constant value that is not 0 or 1 will preferrably be stored as variables (with capitalized names) in the code module. So, we should not print "aa124" directly but revise the naming of the variable as NAME instead of name. At the same time, the constant variable NAME makes the value more reusable and readable as well.

I want to make \ into a string [closed]

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 2 years ago.
Improve this question
Edit= Some moderators recommended me to make my self more clear, so here we go.
As a personal project in python, I'm making a very simple software that asks the user for an email address and then checks if the syntaxis of the email is correct.
I made a tuple of special characters that are not allowed in an email address, one of those characters is "\". I was looking online like crazy for how to make \ into a str with no result. I try looking online for the use of the function \ with no result either.
V = "\" doesn't work, it gives me a syntax error. I know it is possible to make it into a string because I've done it with an Input() command.
Please help.
It's not clear to me what language you're using - but in most cases you need to escape the backslash, as it is an escape character itself.
V="\\"
This functionality exists that you can include special characters (in this case, a double quote) in the string:
V="The following will be in quotes: \"Hello, World\""
In this case, the escaped double quotes will be treated as literal characters in the string, and will not signal the end of the string as they would without the escape character.

Use u'string' on string stored as variable in Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
As a French user of Python 2.7, I'm trying to properly print strings containing accents such as "é", "è", "à", etc. in the Python console.
I already know the trick of using u before the explicit value of a string, such as :
print(u'Université')
which properly prints the last character.
Now, my question is: how can I do the same for a string that is stored as a variable?
Indeed, I know that I could do the following:
mystring = u'Université'
print(mystring)
but the problem is that the value of mystring is bound to be passed into a SQL query (using psycopg2), and therefore I can't afford to store the u inside the value of mystring.
so how could I do something like
"print the unicode value of mystring" ?
The u sigil is not part of the value, it's just a type indicator. To convert a string into a Unicode string, you need to know the encoding.
unicodestring = mystring.decode('utf-8') # or 'latin-1' or ... whatever
and to print it you typically (in Python 2) need to convert back to whatever the system accepts on the output filehandle:
print(unicodestring.encode('utf-8')) # or 'latin-1' or ... whatever
Python 3 clarifies (though not directly simplifies) the situation by keeping Unicode strings and (what is now called) bytes objects separate.

Getting rid of square brackets on either side of a string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am importing currency exchange rates from a website. All is well, except python prints the required data as follows:
['12.3098']
which means I can't use the data that I scraped in any calculations in my program. How do I get rid of the square brackets so that I can convert the string inside the square brackets to a float?
you have a list which is not a string... so if you want to get that value out select it like so
float(result[0])
replace 'result' with whatever your object is, aka what you printed ['12.3098']
try printing the type of your object type(result) and if its a list then this will fix your problem
if the type is a string you can do a literal evaluation of it like this
import ast
result = ast.literal_eval(result)
print result[0]

Categories

Resources