Python基础day-2[流程控制,字符串操作]

流程控制:

  if 语句:

if 条件1:
    (缩进四个空格)条件1成立的话执行的命令
elif条件2:
     前面的条件不成立执行这个语句
else:
     之前的所有条件均不成立的情况下执行的命令
print('1')     //这个print不属于 if判断中

  while语句:

//登录用户输入命令
tag = True              //设置标签
while tag :
    user = input('Please enter your username:')           //等待用户输入
    passwd = input('Please enter your password:')

    if user == 'abc' and passwd == '123':          //判断用户输入
        while tag :               //子循环
            cmd = input('Please enter command:')      //等待用户输入命令
            if cmd == 'exit':              //如果输入exit
                tag = False           //执行 tag = False
                continue       //跳出本层循环
            print(cmd)      //打印出用户输入的命令

  for语句:

for x in range(4):      //循环给 x赋值  范围  #0 1 2 3     //range函数 顾头不顾尾 range(1,5) # 1 2 3 4     步长:range(1,5,2)  #1 3
    print('=====>',x)

#九九乘法表
for x in range(1,10):        //控制行           
    for y in range(1,x+1):      
        print('%s*%s=%s'%(x,y,x*y),end = ' ')        //end= ' ' 打印不换行
    print()  //循环完一次换行

数据类型:

  进制:

    bin():换算十进制为二进制

    oct():换算十进制为八进制

    hex():换算十进制为十六进制

  数字类型的特点:

    1.只能存放一个值

    2.一经定义不能更改

    3.可直接访问

  字符串的类型:

    只要是引号中的都是字符串,单引号双引号在Python中没有区别.

s='hello world'
s1="hello world"
s2="""hello world"""
s3='''hello world'''
print(type(s))
print(type(s1))
print(type(s2))
print(type(s3))

D:\Python\Python36-32\python.exe    #运行结果
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>

Process finished with exit code 0

    字符串的常用操作:

     x.strip()

      name = input('username:')
      name=name.strip()       #删除字符串中头部跟尾部的空格
      x = '==========abc'
      x = x.strip('*')              #指定删除的字符串仅限头部跟尾部
D:\Python\Python36-32\python.exe E:/Python/作业/day2.py
username:*************a***********b********c************
a***********b********c

Process finished with exit code 0

     x.capitalize()    #首字母大写

      x='hello'

      x  = x.capitalize()     

      print(x)

D:\Python\Python36-32\python.exe E:/Python/作业/day2.py
Hello

Process finished with exit code 0

     x.upper()   #字母全部大写

      x='hello'

      x  = x.upper()     

      print(x)

D:\Python\Python36-32\python.exe E:/Python/作业/day2.py
HELLO

Process finished with exit code 0

     x.lower()   #字母全部小写

      x = 'HELLO'
      x = x.lower()
      print(x)

D:\Python\Python36-32\python.exe E:/Python/作业/day2.py
hello

Process finished with exit code 0

     x.center()     #设置字符宽度居中显示

      x = 'HELLO'
      x = x.center(20,'=')    #括号内设置字符宽度为20,填充字符为'='
      print(x)

D:\Python\Python36-32\python.exe E:/Python/作业/day2.py
=======HELLO========

Process finished with exit code 0

     x.count()     #计数,统计字符串中某一字符的个数

      x = 'HELLO,Hello,hello'      #字符从左到右,索引位置分别是:0,1,2,3,4..... 其中空格和标点符号均算做一个字符.
      # x = x.count('e')  #统计字符串中'e'有几个
      x = x.count('e',0,10)  #统计字符串中'e'有几个,统计范围0-10个字符
      print(x)

D:\Python\Python36-32\python.exe E:/Python/作业/day2.py
1

Process finished with exit code 0

     x.endswith()  #从字符串尾部开始判断,是否是相应的值

      x = 'HELLO,Hello,hello '     #尾部有个空格
      x = x.endswith(' ')    #判断是否以空格结尾
      print(x)

D:\Python\Python36-32\python.exe E:/Python/作业/day2.py
True

Process finished with exit code 0

     x.startswith()  #从字符串头部判断,是否是相应的值

      x = 'HELLO,Hello,hello '  
      x = x.startswith('HELLO')  
      print(x)

D:\Python\Python36-32\python.exe E:/Python/作业/day2.py
True

Process finished with exit code 0

     x.find()  #查找字符,如果有就显示找到的第一个字符的索引位置,如果没有就返回一个负数

      x = 'HELLO,Hello,hello'
      x = x.find('e')
      print(x)

D:\Python\Python36-32\python.exe E:/Python/作业/day2.py
7

Process finished with exit code 0

     x.format()  #格式化字符串

      x = 'Name:{},age:{},sex:{}'    #定义一串字符串
      x = x.format('abc',18,'male')    #数值会依次跟上面的大括号对应
      print(x)

      x = 'Name:{0},age:{1},sex:{0}'  #使用数据位置
      x = x.format('aaaaaaaaaaaaaaaaa','bbbbbbbbbbbbbb')
      print(x)

      x = 'Name:{x},age:{y},sex:{z}'   #直接定义括号内的数值,这种方式 format中的数值可以使无序的
      x = x.format(y=18,x='abc',z='male')
      print(x)

D:\Python\Python36-32\python.exe E:/Python/作业/day2.py
Name:abc,age:18,sex:male
Name:aaaaaaaaaaaaaaaaa,age:bbbbbbbbbbbbbb,sex:aaaaaaaaaaaaaaaaa
Name:abc,age:18,sex:male

Process finished with exit code 0

     x[]    #取出字符串对应索引位置的字符,也可以输入 负数从字符串尾部开始计算位置.

      x = 'Hello python'
      x = x[1]
      print(x)
      x = 'Hello python'
      x = x[5]
      print(x)
      x = 'Hello python'
      x = x[0]
      print(x)
      x = 'Hello python'
      x = x[10]
      print(x)

补充:

>>> x = 'Hello python'
>>> x[1:5]
'ello'
>>> x[1:5:2]
'el'
>>> x[0:5:2]
'Hlo'
>>>

D:\Python\Python36-32\python.exe E:/Python/作业/day2.py
e
 
H
o

Process finished with exit code 0

     x.index()  #输出字符的索引位置,如果有多个相同字符则只输出第一个找到的

      x = 'Hello python'
      x = x.index('o')
      print(x)

D:\Python\Python36-32\python.exe E:/Python/作业/day2.py
4

Process finished with exit code 0

     x.isdigit()   #判断是否是数字

      x = 'Hello python'
      x = x.isdigit()
      print(x)

      x = '1231231'
      x = x.isdigit()
      print(x)

D:\Python\Python36-32\python.exe E:/Python/作业/day2.py
False
True

Process finished with exit code 0

     x.replace()  #替换字符

      x = 'Hello python'
      x = x.replace('Hello','Hi')
      print(x)

      x = 'Hello python Hello'
      x = x.replace('Hello','Hi',2)   #括号内的数字代表替换几次,不设定默认只替换找到的第一个
      print(x)

D:\Python\Python36-32\python.exe E:/Python/作业/day2.py
Hi python
Hi python Hi
Process finished with exit code 0

     x.split()   #以什么为分隔符分割字符串

      x = 'Hello python Hello'
      x = x.split()       #不指定,以空格为分隔符进行分割
      print(x)

      x = 'aa*bb*c'
      x = x.split('*')     #指定以'*'号为分隔符进行分割
      print(x)

D:\Python\Python36-32\python.exe E:/Python/作业/day2.py
['Hello', 'python', 'Hello']
['aa', 'bb', 'c']

Process finished with exit code 0

     x.isupper()  #判断字符串是否是大写

      x = 'hello'
      x = x.isupper()
      print(x)

      x = 'HELLO'
      x = x.isupper()
      print(x)

      x = 'Hello'
      x = x.isupper()
      print(x)

D:\Python\Python36-32\python.exe E:/Python/作业/day2.py
False
True
False

Process finished with exit code 0

     x.islower()  #判断字符串是否是小写

      x = 'hello'
      x = x.islower()
      print(x)

      x = 'HELLO'
      x = x.islower()
      print(x)

      x = 'hEllo'
      x = x.islower()
      print(x)

D:\Python\Python36-32\python.exe E:/Python/作业/day2.py
True
False
False

Process finished with exit code 0

     x.isspace()  #判断字符串是否为空格

      x = 'hello'
      x = x.isspace()
      print(x)

      x = ' '
      x = x.isspace()
      print(x)

D:\Python\Python36-32\python.exe E:/Python/作业/day2.py
False
True

Process finished with exit code 0

     x.istitle()  #判断是否是标题,有点像判断首字母是否大写

      x = 'hEllo'
      x = x.istitle()
      print(x)

      x = 'Hello'
      x = x.istitle()
      print(x)

D:\Python\Python36-32\python.exe E:/Python/作业/day2.py
False
True

Process finished with exit code 0

     x.title()  #把字符串转换为标题

      x = 'hEllo'
      x = x.title()
      print(x)

      x = 'hEllO'
      x = x.title()
      print(x)

D:\Python\Python36-32\python.exe E:/Python/作业/day2.py
Hello
Hello

Process finished with exit code 0

     x.swapcase()  #转换大小写

      x = 'hEllo'
      x = x.swapcase()
      print(x)

      x = 'Hello'
      x = x.swapcase()
      print(x)

D:\Python\Python36-32\python.exe E:/Python/作业/day2.py
HeLLO
hELLO

Process finished with exit code 0

     x.ljust()  #左对齐

     x.rjust()  #右对齐

      x = 'hEllo'
      x = x.ljust(10,'=')
      print(x)

      x = 'abc'
      x = x.rjust(10)
      print(x)

D:\Python\Python36-32\python.exe E:/Python/作业/day2.py
hEllo=====
       abc

Process finished with exit code 0

针对上面的内容补充:

  1.x = x.xxxx()

   print(x)

   这种写法也可以这么写:print(x = x.xxxx()),向下面这种示例.

    x='abc'
    print(x.ljust(10,'*'))
    print(x.rjust(10,'*'))

  2.当我们在Python中设置字符串变量时:x = 'abc' 其实就是执行了 :  x = str('abc')

  3.设置变量的时候:a = 1 这时在python中记录的就是 int型数据,a = 'abc' 系统记录的就是 str型数据,只有在 设置input 等待用户输入的数据才会被一律视为 str.

  4.计算字符长度可以使用len(),计数是从1开始计数.字符串索引是从0开始计数.如下示例:

msg='hello egon 666'  
print(len(msg))     #使用len计算出msg的字符数为14
for n in range(14):  #使用range函数生成0-13
    print(msg[n],end = ' ')   #循环打印出索引位置字符,不换行输出

D:\Python\Python36-32\python.exe E:/Python/作业/day2.py
14
h e l l o e g o n 6 6 6
Process finished with exit code 0

   5.2017/06/07  Python练习

#1:编写for循环,利用索引遍历出每一个字符
# msg='hello egon 666'
# print(len(msg))
# for n in range(14):
#     print(msg[n],end = ' ')

#2:编写while循环,利用索引遍历出每一个字符
# msg='hello egon 666'
# n = 0
# while n < 14:
#     print(msg[n],end = ' ')
#     n += 1

#3:msg='hello alex'中的alex替换成SB
# msg='hello alex'
# print(msg.replace('alex','SB'))

#4:msg='/etc/a.txt|365|get' 将该字符的文件名,文件大小,操作方法切割出来
# msg='/etc/a.txt|365|get'
# s1 = msg.split('|')
# print(s1)
# print('文件名是{},文件大小是{},操作方法是{}'.format(*s1))
# print('文件名是%s,文件大小是%s,操作方法是%s'%tuple(s1))

#5.编写while循环,要求用户输入命令,如果命令为空,则继续输入
# tag = True
# while tag:
#     cmd = input('Please enter command: ')
#     if cmd == '':
#         continue
#     else:
#         print(cmd)
#         tag = False

#6.编写while循环,让用户输入用户名和密码,如果用户为空或者数字,则重新输入
# tag = True
# while tag:
#     user = input('Please enter your username:')
#     passwd = input('Please enter your password:')
#     if user == '' and passwd == '':
#         continue
#     elif user == 'abc' and passwd == '123':
#         print('login successful')
#         tag = False
#     else:
#         print('login fail')
#         tag = False

#7.编写while循环,让用户输入内容,判断输入的内容以alex开头的,则将该字符串加上_SB结尾
# while True:
#     name = input('Please enter your name:')
#     if name.startswith('alex'):
#         print(name,'SB')
#         break
#     else:
#         print('hello',name)

#8.下列要求:
#1.两层while循环,外层的while循环,让用户输入用户名、密码、工作了几个月、每月的工资(整数),用户名或密码为空,或者工作的月数不为整数,或者
#月工资不为整数,则重新输入
#2.认证成功,进入下一层while循环,打印命令提示,有查询总工资,查询用户身份(如果用户名为alex则打印super user,如果用户名为yuanhao或者wupeiqi
#则打印normal user,其余情况均打印unkown user),退出功能
#3.要求用户输入退出,则退出所有循环(使用tag的方式)
#
# tag = True
# while tag:
#     user = input('请输入用户名:')
#     passwd = input('密码:')
#     work_mons = input('工作了几个月?:')
#     salary = input('请输入你每月的工资(整数):')
#     if user == '' or passwd == '' or work_mons.isdigit() != True or salary.isdigit() != True:
#         print('输入数据错误,请重新输入.')
#         continue
#     else:
#         print('登录成功')
#         while tag:
#             print('''
#             1.查询总工资
#             2.查询用户身份
#             3.退出登录
#             ''')
#             n = input('>>:')
#             if n == '1':
#                 # work_mons = int(work_mons)
#                 # salary = int(salary)
#                 #money = int(work_mons) * int(salary)
#                 #print('你的总工资是:',money)
#                 print('你的总工资是:',int(work_mons) * int(salary))
#             elif n == '2' and user == 'alex':
#                 print('super user')
#             elif n == '2' and (user == 'yuanhao' or user == 'wupeiqi'):
#                 print('normal user')
#             elif n == '2':
#                 print('unknow user')
#             elif n == '3':
#                 tag = False
Python练习

 

 

 

posted @ 2017-06-07 17:27  neuropathy_ldsly  阅读(239)  评论(0)    收藏  举报