注释
单行注释 #
多行注释 ''' 三个单引号或者三个双引号 """

''' 用三引号引住可以多行赋值

用户交互 input

字符串拼接
+  ""%() "".format()推荐使用
name = input("name:")
age = int(input("age:"))
sex = input("sex:")
例:+
# 字符串拼接+

info1 = '''----info in ''' + name + '''---
name:''' + name + '''
age:''' + age + '''
sex:''' + sex + '''
'''

例:""%()
# %格式化字符串

info = '''------info in %s -------
name:%s
age:%d
sex:%s
''' % ("name", "name", age, "sex")

#"".format()

字符串格式化 format    []都是可选的,可填可不填
格式:[[fill][align][sign][#][0][width][,][.precision][type]
fill  填充字符      
align 对齐方式        
    ^ 居中            s = "{:-^20d}".format(20)            -----20-----
    < 内容左对齐    s = "{:-<20d}".format(20)            20----------
    > 内容右对齐    s = "{:->20d}".format(20)            ----------20
    = 内容右对齐,将符号放在填充字符的左侧,只对数字有效
sign  有无符号数字(感觉用处不大)
    +       正数加+ 负数加-        s = "{:+d} this is Numbers".format(-20)        -20
    -        正数不变  负数加-    s = "{:-d} this is numbers".format(23)         23
    空格    正数空格   负数加-  s = "{: d} this is numbers".format(23)      23
#     对数字有效,对于二、八、十六进制,会对应显示 0b 0o 0x    s = "{:#0x}".format(213)
width 格式化字符宽度            s = "{:-^20d}".format(20)
,     对大的数字有效,添加分隔符 如1,000,000    s = "{:,d}".format(2000000000)   2,000,000,000
.precision  小数位保留精度    s = "{:.2f}".format(12.2323)        12.23
type  格式化类型
    s 字符串     s = "this is {}".format("string")
    b 十进制转二进制表示 然后格式化   s = "{:d}".format(23)   10111
    d 十进制
    o 十进制转八进制表示 然后格式化        s = "{:o}".format(23)   27
    x 十进制转十六进制表示 然后格式化   s = "{:x}".format(23)   17
    f 浮点型 默认小数点保留6位
    % 显示百分比 默认小数点后6位         s = "{:.2%}".format(0.1234) 参数可用[]及{} ,使用时必须加*,**
s = "my name is {},age is {}".format(*["niu", 25]) s = "my name is {name}, age is {age}".format(**{"name": "niu", "age": 25})
info3 = '''---info in {_name}---
name:{_name}
age:{_age}
sex:{_sex}
'''.format(_name=name,
_age=age,
_sex=sex)
info4 = '''---info in {0}---
name:{0}
age:{1}
sex:{2}'''.format(name, age, sex)

模块定义:
密文密码:getpass  引用后使用,getpass.getpass()
if else 使用
例:

username = "username"
password = ""
_Username = input("Username:")
_Passwd = input("Password:")
if username == _Username and password == _Passwd:
print("welcome user {name} to beij".format(name=username))
else:
print("Invalid username or passwd")

if elif else
例:

Myage = 37
InputAge = int(input("please input my age:"))
if InputAge == Myage:
print("It's right")
elif InputAge > Myage:
print("Think small")
else:
print("Think big") While else 循环
count = 0
while count < 3:
Myage = 37
InputAge = int(input("please input my age:"))
if InputAge == Myage:
print("It's right")
break
elif InputAge > Myage:
print("Think small")
else:
print("Think big")
count+=1
else:
print("fuck you!")

break    跳出当前整个循环
continue 跳出当前循环,进入下次循环

作业

编写登陆接口

  • 输入用户名密码
  • 认证成功后显示欢迎信息
  • 输错三次后锁定

old_uname = open(r'C:\Users\Administrator\Desktop\username.txt', 'r').readlines()
count = 0
while count < 3:
username = input("please your username:")
passwd = input("please your passwd:")
for i in old_uname:
if i == username:
print("wolcome to your blogs:{_uname}".format(_uneme=username))
break
else:
continue
else:
count += 1
if count == 3:
continue
print("The password you entered is incorrect!please input again...")
else:
print("三次错误,账号已锁定")
open(r'C:\Users\Administrator\Desktop\lockname.txt', 'a').write(username + '\n')

字符串基础
#字符串操作
#去掉空格 strip () 括号内可指定 默认是空格
username = input("username:")
if username.strip('-') == "zhang":
print(username.strip('-'))
print("welcome %s to beijing" % username)

# 分割 split 可指定根据什么分割
list11 = "welcome to beijing"
list2 = list11.split()
print(list2) # ['welcome', 'to', 'beijing']

# 合并 join 可指定根据什么合并
list3 = ":".join(list2) + '\n'  # welcome:to:beijing
list4 = " ".join(list2) # welcome to beijing
print(list3, list4)
# 判断有没有空格\切片
name = "mr,niu"
print("," in name)
name1 = "mr niu"
print(" " in name1)
print(name[2:4])

# format 字符串格式化
men = "my name is {name}, age is {age}"
all = men.format(name="niu", age=23) men1 = "my name is {0}, age is {1}"
all1 = men1.format("niu", 23)
print(all1) fill = "niu"
fill.isdigit() # 判断是不是数字
fill.endswith('') # 判断是不是以指定字符串结尾
fill.startswith('') # 判断是不是以指定字符串开头
fill.upper()   # 批量转换成大写
fill.lower() # 批量转换成小写
fill.capitalize() # 第一个字母大写,其他都小写
fill.title() # 每个单词的首字母大写,其他都小写
print(fill.center(20, '='))
(1)、转义字符串             
(2)、raw字符串--转义机制  open(r'c:\tmp\a.txt','a+')
(3)、Unicode字符串
(4)、格式化字符串  "age %d,sex %s,record %m.nf"%(20,"man",73.45)
字符串基础操作
 + 连接 、* 重复、s[i] 索引(index)、s[i:j] 切片(slice)、for循环遍历

Python 基础 字符串拼接 + if while for循环的更多相关文章

  1. python基础——字符串和编码

    python基础——字符串和编码 字符串也是一种数据类型,但是,字符串比较特殊的是还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用 ...

  2. Python 全栈开发二 python基础 字符串 字典 集合

    一.字符串 1,在python中,字符串是最为常见的数据类型,一般情况下用引号来创建字符串. >>ch = "wallace" >>ch1 = 'walla ...

  3. Python 基础-> 字符串,数字,变量

    Python 基础:字符串,数字,变量 1. 字符串 (信息的一种表达方式) a. 使用引号创建字符串 b. 单引号,双引号,三引号: ', ", ''', ""&quo ...

  4. Python基础-字符串格式化_百分号方式_format方式

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  5. Python中字符串拼接的三种方式

    在Python中,我们经常会遇到字符串的拼接问题,在这里我总结了三种字符串的拼接方式:     1.使用加号(+)号进行拼接 加号(+)号拼接是我第一次学习Python常用的方法,我们只需要把我们要加 ...

  6. Python基础——字符串

    Python版本:3.6.2  操作系统:Windows  作者:SmallWZQ 在Python中,字符串也是一种数据类型.相比其它数据类型,字符串算是比较复杂的.为何呢?因为字符串不仅包含英文字母 ...

  7. Python中字符串拼接的N种方法

    python拼接字符串一般有以下几种方法: ①直接通过(+)操作符拼接 s = 'Hello'+' '+'World'+'!'print(s) 输出结果:Hello World! 使用这种方式进行字符 ...

  8. Python基础:字符串(string)

    字符串的常用操作 字符串与数组一样,支持索引操作.切片与遍历 索引.切片操作: name = 'jason' name[0] 'j' name[1:3] 'as' 遍历: for char in na ...

  9. Python基础(6)--条件、循环

    本文的主要内容是 Python 的条件和循环语句以及与它们相关的部分. 我们会深入探讨if, while, for以及与他们相搭配的else,elif,break,continue和pass语句. 本 ...

随机推荐

  1. Codeblocks + opencv + Cmake + minGW 环境搭建(一劳永逸版)

    应工作开发需要,今天搭建一个codeblocks的C++开发环境,需要配置opencv2.4.4的API协同开发. 1.为了避免不必要的配置编译器,下载codeblocks16.1带mingw编译器版 ...

  2. ROS学习笔记(九)——ROSSERVICE

    NEW 1 $ roscore NEW 2 $ rosrun turtlesim turtlesim_node NEW 3 $ rosrun turtlesim turtle_teleop_key N ...

  3. 基于特定领域国土GIS应用框架设计及应用

              基于特定领域国土GIS应用框架 设计及应用              何仕国 2012年8月16日   摘要: 本文首先讲述了什么是框架和特定领域框架,以及与国土GIS 这个特定领 ...

  4. 2014 百度之星题解 1002 - Disk Schedule

    Problem Description 有非常多从磁盘读取数据的需求,包含顺序读取.随机读取.为了提高效率,须要人为安排磁盘读取.然而,在现实中,这样的做法非常复杂.我们考虑一个相对简单的场景. 磁盘 ...

  5. java连接oracle的简单实例

    连接oracle的时候,要导入oracle驱动的jar包. 连接的时候,有statement和preparedstatement两种,从代码中可以看出不同. example: package com. ...

  6. Day04 - Python 迭代器、装饰器、软件开发规范

    1. 列表生成式 实现对列表中每个数值都加一 第一种,使用for循环,取列表中的值,值加一后,添加到一空列表中,并将新列表赋值给原列表 >>> a = [0, 1, 2, 3, 4, ...

  7. XC一键锁屏应用

    XC一键锁屏,一键Android锁屏应用,彻底解放开关机键~ 下载地址: http://download.csdn.net/detail/jczmdeveloper/7329447

  8. Android(java)学习笔记195:三重for循环的优化(Java面试题)

    1.首先我们看一段代码: for(int i=0;i<1000;i++){ for(int j=0;j<100;j++){ for(int k=0;k<10;k++){ testFu ...

  9. CSS hack常用方案(摘选)

    邮箱因为默认了line-height?:170%,导致采用table元素时继承问题,可以采用line-height:50% 很好解决. 常 在使用float时,后面的显示不正常,因为继承了float了 ...

  10. php 白屏

    访问php白屏(base on lnmp) vim nginx/conf/fastcgi_param fastcgi_param REDIRECT_STATUS 200; fastcgi_param ...