擅长:python、mysql、java
<p>最简单的方法是跟踪要大写的索引,并分三个阶段打印:</p>
<ol>
<li>字符串中<em>在</em>之前要大写的部分</li>
<li>要大写的字符,已转换为大写</li>
<li>字符串<em>中要大写的</em>后面的部分</li>
</ol>
<p>我们可以用细绳切片来做这个。你知道吗</p>
<pre><code>import os, time
string = "this is a test"
count = 0
while True:
# clear screen
os.system('clear')
# print each of the three section
first_part = string[:count]
second_part = string[count].upper()
third_part = string[count + 1:]
print(first_part + second_part + third_part)
# increment to the next index. Once we reach the end of the string, wrap around.
count = (count + 1) % len(string)
time.sleep(0.1)
</code></pre>