在单独的行上打印出字符串的长度 [英] Print out the length of strings on seperate lines
问题描述
所以我有一个名为fruits.txt的文件,里面是:
pear
apple
orange
普通话
西瓜
石榴
我的目标是打印不同行上每个单词的长度。所以结果应该是这样的:
4
5
6
8
10
11
我在cmd行中所做的是
file = open( fruits.txt, r)
content = file.read()
file.close()
answer = len(内容) i 内容
这给了我一个错误SyntaxError:invalid syntax并指向for中的r。我该如何解决这个问题呢?
我尝试了什么:
< pre lang =Python>
file = open(fruits.txt,r)
content = file.read()
file.close()
answer = len(content)for i in content
answer = len(内容) for i in content
这不是有效的Python语句;请参阅 4。更多控制流工具 - Python 3.4.9文档 [ ^ ]。
另见7。输入和输出 - Python 3.7.1rc1文档 [ ^ ]。
So I have a file named fruits.txt and inside is:
pear
apple
orange
mandarin
watermelon
pomegranate
my goal is to print out the length of each word on different lines. So the result should look like this:
4
5
6
8
10
11
what I did in the cmd line is
file = open("fruits.txt", "r")
content = file.read()
file.close()
answer = len(content) for i in content
This gave me an error "SyntaxError: invalid syntax" and pointed to the "r" in "for". How would i solve this issue?
What I have tried:
<pre lang="Python"> file = open("fruits.txt", "r") content = file.read() file.close() answer = len(content) for i in content
answer = len(content) for i in content
That is not a valid Python statement; see 4. More Control Flow Tools — Python 3.4.9 documentation[^].
See also section 7.2.1 at 7. Input and Output — Python 3.7.1rc1 documentation[^].
这篇关于在单独的行上打印出字符串的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!