简体   繁体   English

Visual Studio 在将字符串与数字组合时出现语法错误

[英]visual studio issue syntax error with combining string to number

currently having some issues with visual code that dont make sense.目前在视觉代码方面存在一些没有意义的问题。 tried printing out the following sentence in python:尝试在python中打印出以下句子:

 count = 0
 message = f"We have {count} even numbers."
 print(message)

With the following error:出现以下错误:

message = f"We have {count} even numbers."
                                        ^
invalid syntax

I'm just wondering what the correct would be, do i need to add a + around the count like java?我只是想知道正确的是什么,我是否需要像java一样在计数周围添加一个+?

It's probably because you're using a version that is under 3.6, if so, use %s string formatting:可能是因为您使用的版本低于 3.6,如果是这样,请使用%s字符串格式:

count = 0
message = "We have %s even numbers." % count
print(message)

Or use str.format :或使用str.format

count = 0
message = "We have {} even numbers.".format(count)
print(message)

It works even for pyhton 3.5 or less它甚至适用于 pyhton 3.5 或更低版本

num = 10
string_data = "you have {} apples.".format(num)
print(string_data)

暂无
暂无

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

相关问题 在 python 中组合 json 字符串时出现语法错误 - syntax error occured in combining json string in python 类问题 - 程序按预期工作,但 Visual Studio 代码突出显示语法错误 - Issue with Classes - Program works as intended, but visual studio code is highlighting syntax error 未知空间的Visual Studio代码语法错误 - visual studio code syntax error with unknown space bash:Visual Studio Code 中意外标记“&”附近的语法错误 - bash: syntax error near unexpected token `&' in Visual Studio Code 将python的默认错误语法解析为Visual Studio的错误窗口语法 - Parsing python's default error syntax to Visual Studio's error window syntax mac 上的 Visual Studio Code 使用 python 错误“syntax error: invalid syntax”在“cerebro = bt.Cerebro()”行 - Visual Studio Code on mac using python Error "syntax error: invalid syntax" at the line 'cerebro = bt.Cerebro()' Visual Studio 代码上的“没有名为 pandas 的模块”错误 - jupyter notebook 上没有问题 - "No module named pandas" error on visual studio code - NO ISSUE on jupyter notebook Visual Studio 代码 - 终端问题 - Visual studio code - terminal issue Visual Studio Code (VSC) python bash:意外标记“&”附近的语法错误 - Visual Studio Code (VSC) python bash: syntax error near unexpected token `&' 从 Visual Studio Code 内部运行 python 时出现无效语法错误 - Invalid Syntax error when running python from inside Visual Studio Code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM