【发布时间】:2019-04-04 11:29:43
【问题描述】:
所以我有这个作业问题:
将school_prompt.txt 的前30 个字符作为字符串 分配给变量beginning_chars。
在上一个问题中,我设法计算了 txt 文件中的所有字符,但我不知道如何将前 30 个添加到变量中。
fname = "school_prompt.txt"
lines = 0
nwords = 0
beginning_chars = 0
with open(fname, 'r') as f:
for line in f:
if line >= 30:
words = line.split()
lines +=1
nwords += len(words)
beginning_chars += len(line)
【问题讨论】:
标签: python string variables character add