Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm trying to keep a string I've converted from an integer. I can convert an int variable into a string, but then I can't seem to interact with that. Here's how it goes in the Python shell:
a = 100
a
>>> 100
str(a)
>>> '100'
a
>>> 100
str(a) = b
>>> SyntaxError: can't assign to function call
What I need is to turn this '100' string into a new variable. I've tried searching; the answer is probably out there, but I'm clearly not using the right search terms. All the answers I've found have only been concerned with how to convert from one type to another.
The problem you are experiencing is not the problem that you think you are experiencing.
When you define a variable in Python, the variable goes on the left of the equals sign. It should look like this:
b = str(a)
This will define b without giving you an error message. Going back to your question, if you want to change a to a string and keep it that way, str(a) will not suffice. Instead:
a = str(a)
Will change your variable to a string. str(a) is simply a function that returns the variable a in the form of a string. If you do not redefine a here, str(a) will not return to anywhere and your string will be lost.
You have your assignment syntax back to front. The target name goes on the left:
b = str(a)
Now b is a reference to the return value of str().
You can also re-assign back to a, replacing the old integer value with the string representation:
a = str(a)
Your attempt instead tried to use str(a) as an assignment target; Python can't let you do that because the result of str(a) is unknown when the code is compiled; you cannot bind any object to another, you need to have a reference instead (so a name, or an attribute, or a list index, or a dictionary key, etc.).
Related
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 6 months ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I was working with f-strings, and I am fairly new to python. My question is does the f-string formatting cast a variable(an integer) into a string?
number = 10
print(f"{number} is not a string")
Is number cast into a string?
f"..." expressions format values to strings, integrate the result into a larger string and return that result. That's not quite the same as 'casting'*.
number is an expression here, one that happens to produce an integer object. The integer is then formatted to a string, by calling the __format__ method on that object, with the first argument, a string containing a format specifier, left empty:
>>> number = 10
>>> number.__format__('')
'10'
We'll get to the format specifier later.
The original integer object, 10, didn't change here, it remains an integer, and .__format__() just returned a new object, a string:
>>> f"{number} is not a string"
'10 is not a string'
>>> number
10
>>> type(number)
int
There are more options available to influence the output by adding a format specifier to the {...} placeholder, after a :. You can see what exact options you have by reading the Format Specification Mini Language documentation; this documents what the Python standard types might expect as format specifiers.
For example, you could specify that the number should be left-aligned in a string that's at least 5 characters wide, with the specifier <5:
>>> f"Align the number between brackets: [{number:<5}]"
'Align the number between brackets: [10 ]'
The {number:<5} part tells Python to take the output of number.__format__("<5") and use that as the string value to combine with the rest of the string, in between the [ and ] parts.
This still doesn't change what the number variable references, however.
*: In technical terms, casting means changing the type of a variable. This is not something you do in Python because Python variables don't have a type. Python variables are references to objects, and in Python it is those objects that have a type. You can change what a variable references, e.g. number = str(number) would replace the integer with a string object, but that's not casting either.
You can test it yourself quite easily
number = 10
print(f"{number} is not a string")
print(type(number))
# output is integer
If u can't understand f string or other soln dosent make any sense then try this.
This will help you integrate a variable(integer/char) in a string.
a = "blah blah blah {} blah blah ".format(integer_value)
print(a)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
V=str(input ('\n\tEnter\n\n\t'))
if V.isnumeric:
print (f'\n\tYour V is a numeric and it is "{V}"\n\t')
When I enter an alphabet, it considers it as a numeric. How to make it only accept numbers or alphabets?
isnumeric is a function but you are not calling it so it is returning true as it is a defined function. Your if statement is checking whether isnumeric is defined for the variable V
You should do this -
V=str(input ('\n\tEnter\n\n\t'))
if V.isnumeric():
print (f'\n\tYour V is a numeric and it is "{V}"\n\t')
I think it should be V.isnumeric() there is some limitations on negative
Definition and Usage
The isnumeric() method returns True if all the characters are numeric (0-9), otherwise False.
Exponents, like ² and ¾ are also considered to be numeric values.
"-1" and "1.5" are NOT considered numeric values, because all the characters in the string must be numeric, and the - and the . are not.
This should be useful if you want to try a couple of functions: https://lerner.co.il/2019/02/17/pythons-str-isdigit-vs-str-isnumeric/
The function isnumeric() is not being called in your code presently, you are simply referencing it:
if v.isnumeric: # This resolves to a pointer to the isnumeric function
....
To call a function, you require v.isnumeric(). Note that in the original (without parentheses) it resolves to function object, which in turn evaluates to True (essentially, because it exists at all) when in an if clause. However, the function itself is never run. It's for this reason that you're seeing it print out every time.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I know the title is a bit confusing, but what it means is this:
abdc123 is my reference string and I want to know if this string appears on all other strings. Here's the code:
if ref_string in target_string:
print("True")
if target_string says something like "abcd123 asdf789 bnc1222", it will return as True.
However if target_string says something like "asdf789 bnc1222 abcd123", it will now say False even if abcd123 is clearly there. It only works if abcd123 is the first string? How can I fix this? Thanks a lot!
PS: target_string is a string, not a list. It's like a sentence.
If I take the reference and target strings which you specified, I get a True as expected:
ref_string = "abcd123"
target_string = "asdf789 bnc1222 abcd123"
print(ref_string in target_string)
This outputs True.
Note that in the opening line to your question, you've referenced a subtly different string (abdc123), so do make sure to check that your reference string really is in the target.
Split the string target_string which casts it to a list and then compare ref_string with that list as follows:
ref_string='abdc123'
target_string="abdc123 asdf789 bnc1222"
a=target_string.split()
if ref_string in a:
print("True")
else:
print('False')
Edit: As mentioned in the comment in works on the string as well, and yes that is another method and it works fine too.
ref_string='abdc123'
target_string="asdf789 dfbfb abdc123"
if ref_string in target_string:
print("True")
else:
print('False')
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
This code:
a = 10
b = 20
print(f "The variable a is {a} and the variable b is {b}.")
returns this error:
File "main.py", line 3
print (f "The variable a is {a} and the variable b is {b}")
^
SyntaxError: invalid syntax
The version I'm using is after 3.6, so it should work. I'm using Anaconda's prompt, if that's a problem.
you have a space between f and the string. Remove it and everything will work.
The syntax error points to the end of the string. If it pointed to the beginning of the string it would give you a better hint at the problem: after the f, which is interpreted as a variable in this case, a string is unexpected.
You need this:
a = 1
b = 2
test_str = f"{'The variable a is'} {a} {'and the variable b is '}{b}"
print(test_str)
Between the curly braces, string interpolation takes place. The variables for a and b are within two of the curly brace sets and hard-coded strings are within the other two.
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 5 years ago.
Improve this question
While learning python, I could not find the difference between the use of str() and " ".
First code
Second code
With str() function you are changing the number type to String but with "" you just pass the String.
str(3.14) # 3.14 is a number and your are converting it into String.
"3.14" is an String value.
Imagine if you had a variable
pi=3.14
then
str(pi)
would give the result 3.14
whereas
"pi"
would give the result pi. The str() function converts something to its string form. Whereas simple quotes will return the word itself.
str() returns a string representation of an object, while quotation marks indicate the value is a string. To see the difference, consider the following:
x = 3.14
print("x") #outputs the character x
print(str(x)) #string representation of the value of object x
In the first print(), the actual character 'x' is output. This has nothing to with the variable x. However, in the second print(), the value of the object x is converted to a string, so '3.14' is output.
There is no difference between the two. The program will run the same. CodeCademy requires that you use the skills (functions and methods) that it teaches you in that step in order to progress to the next one. The python script doesn't do anything differently, but the CodeCademy code analyzer notices that you didn't accomplish the task the way they wanted you too.