简体   繁体   English

如何将特殊字符的字符串类型转换为原始字符?

[英]how can I convert string type of special character into original?

I wanted to do maths calculation using asterick (*) but what if it is in string format?我想使用星号 (*) 进行数学计算,但如果它是字符串格式怎么办? how can I convert it to normal?我怎样才能将它转换为正常?

I tried 4 "*" 5 and first of all, I was not even expecting it to multiply it, as the operator is in string format but it gave me an error.我尝试了 4 "*" 5 ,首先,我什至没想到它会乘以它,因为运算符是字符串格式,但它给了我一个错误。

An asterisk in the form of a string variable cannot be converted directly to a mathematical operator;字符串变量形式的星号不能直接转换为数学运算符; however, it is possible to take the string "*" and use it to perform multiplication by using an if statement.但是,可以获取字符串“*”并使用它通过 if 语句执行乘法。

Let's say you are given some string variable 'operator', and two integer variables 'a' and 'b'.假设您获得了一些字符串变量“operator”和两个 integer 变量“a”和“b”。 The code would look something like this:代码看起来像这样:

if operator == "*":
     product = a * b
     print(product)

In your case, you would set the 'a' and 'b' variables to 4 and 5 respectively, meaning the example above would print 20 if the 'operator' variable was "*".在您的例子中,您可以将 'a' 和 'b' 变量分别设置为 4 和 5,这意味着如果 'operator' 变量为“*”,上面的示例将打印 20。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM