This question already has answers here:
Evaluating a mathematical expression in a string
(14 answers)
Safely evaluate simple string equation
(4 answers)
Closed 2 years ago.
Say you have:
text = "22 + 33"
And you want to verify that the sign used it is actually an addition sign:
for word in text:
If word == "+":
How do I find the index/position of the addition sign inside the string?
How do you find the index/position of the numbers to the sides of the addition sign?
I want to do this to then assign these values to variables and then be able to calculate the problem.
PD: The problem is that, the string is not only going to have the addition and thats it, it will contain much more words and signs. Plus, it will be constantly updating (every 0.5 secs). Thats why I want to locate if there is an ADDITION sign and then find the two numbers to its sides to consecuently calculate the sum. Therefore, following my approach, I should need the indexes of the the three things; number1, addition sign and number 2.
You can use the eval() function in Python to evaluate an expression.
For example, consider the following code:
text = "22 + 33"
print(eval(text))
This will print out 55, which is 22 + 33
Related
This question already has answers here:
How to add a string in a certain position?
(10 answers)
Closed 7 months ago.
number = input("Enter the number:")
''' Let's suppose the entered number is 0145. Question is below.'''
I want to add a comma after 0.
How can i do this?
Use {:,} to format a number with commas in Python.
Example:
number = 1000
print(f"{number:,}")
Output:
1,000
If want a general purpose number formatter for numbers as strings that may include leading 0's then there is a solution using regular expressions here.
If user enters "0145" and want to format that as "0,145" then you'd need to use the string input rather than converting to an integer which would drop any leading 0's.
This question already has answers here:
Evaluating a mathematical expression in a string
(14 answers)
Closed 8 months ago.
Let's say I have a string named product.
product may be string such as "5*2" or "12/3".
Does anyone know a way I can compute the output of a product or dividend such as this? Considering sum is a string.
check whether "/" or "*" are in your string and split it accordingly :)
product = "12/3"
if "*" in product:
first,sec = product.split("*")
print(float(first)*float(sec))
elif "/" in product:
first,sec = product.split("/")
print(float(first)/float(sec))
This question already has answers here:
Understanding slicing
(38 answers)
Closed 2 years ago.
Assuming the string has at least one character in it. If the string length is odd, then you may assume it returns (𝑛−1)/2 characters where n represents how many characters are in the original string.
For example:
'small' => 'sm'
I would also like to write another function that returns the 2nd half of a string.
You can just do an integer division (//) to get the integer value of the division to get the desired (n-1)/2 value for odd n. Therefore, having the following:
>>> my_string = "small"
>>> print(my_string[:len(my_string)//2])
sm
You could do the similar thing with using math.floor to be more explicit, but result is the same.
This question already has answers here:
String count with overlapping occurrences [closed]
(25 answers)
Closed 4 years ago.
im just learning how to program on edx, and theres this question that i just can seem to understand not to even talk of solving. please i need solutions and idea on how to understand the question and implement it
QUESTION-
Assume s is a string of lower case characters.
Write a program that prints the number of times the string 'bob' occurs in s. For example, if s = 'azcbobobegghakl', then your program should print
Number of times bob occurs is: 2
Since you are just learning, start with basic method.
Simplest apporch will be start from the begining consider all the substrings of the length of the substring that is needed to be counted. If there is a match increase the count.
You can refer the following code to get an idea.
s = 'azcbobobegghakl'
sb = 'bob'
results = 0
sub_len = len(sb)
for i in range(len(s)):
if s[i:i+sub_len] == sb:
results += 1
print(results)
This question already has answers here:
create a string of variable length in python
(3 answers)
Python Spaced Triangle
(4 answers)
Closed 6 years ago.
Here is my current code. The question is exactly in the title. (I'm looking to be able to align my triangle correctly)
def Triangle(height):
if height % 2 == 0:
print "Please enter a height that is odd"
else:
stringTriangle = "x"
for x in range(height):
print stringTriangle
for x in range(1):
stringTriangle+="xx"
To make a string containing n spaces, use " "*n. To concatenate two strings a and b, use a + b. To learn about other things that can be done with strings, see Python 2: Built-in Types: str and Other Sequence Types.
From there, you should be able to solve your homework problem by calculating how many spaces and how many asterisks you need in each line of the figure.