for循环;range,len函数;in操作符

#for循环
word = input("Enter a word:")
print("\nHere's each letter in your word:")
for letter in word:
    print(letter)
    
Enter a word:lzq

Here's each letter in your word:
l
z
q
#range函数
print("Counting:")
for i in range(10):
    print(i,end=" ")
    
print("\n\nCounting by five:")
for i in range(0,50,5):
    print(i,end=" ")
    
print("\n\nCounting backwards:")
for i in range(10,0,-1):
    print(i,end=" ")
    
Counting:
0 1 2 3 4 5 6 7 8 9 

Counting by five:
0 5 10 15 20 25 30 35 40 45 

Counting backwards:
10 9 8 7 6 5 4 3 2 1 
# len()函数和in运算符
message = input("Enter a message:")

print("\nThe length of your message is:",len(message))

print("\nThe most common letter in the English language'e'")
if "e" in message:
    print("is in your message.")
else:
    print("is not in your message.")
    
Enter a message:lzq like to study.

The length of your message is: 18

The most common letter in the English language'e'
is in your message.
#字符串的索引
import random

word = "index"
print("The word is:%s\n"%word)

high = len(word)
low = -len(word)

for i in range(10):
    position = random.randrange(low,high)  #不包含high
    print("word[%d]:%s"%(position,word[position]))
    
The word is:index

word[2]:d
word[-4]:n
word[3]:e
word[-1]:x
word[3]:e
word[1]:n
word[-1]:x
word[-2]:e
word[-5]:i
word[3]:e

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值