码迷,mamicode.com
首页 > 编程语言 > 详细

python 基础应用3

时间:2020-04-28 17:31:41      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:span   使用   spl   SHH   bsp   strip   dig   div   字符串   

1、使用while 、for循环分别打印字符串s = ‘fsufhjshh3hf‘中每一个元素。

#使用while 、for循环分别打印字符串s = ‘fsufhjshh3hf‘中每一个元素。
s = fsufhjshh3hf
for i in s:
    print(i)
或
index = 0
while 1:
    print(s[index])
    index += 1
    if index == len(s):
        break# f s u f h j s h h 3 h f

2、如:content = input(‘请输入内容:‘) #如用户输入:5+9或5 +9或 5 + 9,然后进行分割再进行计算·。

#如:content = input(‘请输入内容:‘)   #如用户输入:5+9或5  +9或  5  +  9,然后进行分割再进行计算·。
content = input(请输入内容:)
index = content.split(+)
a = index[0].strip()
b = index[1].strip()
sum = int(a)+int(b)#      5    +   9
print(sum) #14

#如:content = input(‘请输入内容:‘)   #如用户输入:5+9或5  +9或  5  +  9,然后进行分割再进行计算·。
content = input(请输入内容:).strip()
index = content.find("+")
a = int(content[0:index])
b = int(content[index+1:]) #      5    +   9
print(a+b)   #14

 

#n个数叠加
content = input(请输入内容:).strip()
index = content.split(+)
sum = 0
for i in index:
    sum+=int(i)  #  2  + 8  +  7   
print(sum)#17

 3、任意输入一串文字+数字 统计数字个数。

#任意输入一串文字+数字 统计数字个数。
s = input(请输入:)
count = 0
for i in s:
    if i.isdigit():
        count += 1#d2qgqtr76768yduqgdy3
print(count)#7

 

python 基础应用3

标签:span   使用   spl   SHH   bsp   strip   dig   div   字符串   

原文地址:https://www.cnblogs.com/2584808136-qq-com/p/12788638.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!