# "adcbdefg"
# "a" # 判断某个东西是否在里面包含 in | not in # name = "abcdefg"
#
# if "adc" in name:
# print("ok")
# else :
# print("EROOR") # ctrl + ? 是注释 # 布尔值 真 true 假 false
"""
比较运算符
==
>
<
>=
<=
!= <> 不等于 逻辑运算符
and
or
not 取反
多运算需加()
执行顺序
从前到后
TRUE or ==> true
true and ==> go on
False or ==> go on
False and ==> False 成员运算
in
not in """ """
赋值运算和算数运算 一样
a = a + 1 a += 1 + +=
- -=
* *=
/ /=
% %=
** **=
// //=
""" """
基本数据类型
数字
int 整形(3)
long 长整形(2) - int
将字符串转变为数字
a = "123"
print(a, type(a))
b = int(a)
print(type(b),b) num = "a"
v = int(num,base=16)
print(v)
-bit_length
age = 3
r = age.bit_length()
print(r)
字符串 str 列表 list 元组 tuple 字典 dict 布尔值 bool """ # 字符串的魔法 test = "cuihaiCHENG"
v1 = test.capitalize()   # 首字母大写,其他小写
print(v1) v2 = test.casefold()   # 所有变小写,他更牛逼
v3 = test.lower()   # 所有变小写
print(v2)
print(v3) v4 = test.center(20, "-") # 设置宽度,并将内容剧中,20为总长度,-为空白处为知填充,一个字符可有可无
print(v4) v25 = test.ljust(20, "-") # 设置宽度,并将内容剧左,20为总长度,-为空白处为知填充,一个字符可有可无
v26 = test.rjust(20,'-') # 设置宽度,并将内容剧右,20为总长度,-为空白处为知填充,一个字符可有可无
v27 = test.zfill(20)
print(v25, '\n', v26, '\n', v27) v5 = test.count("i", 3, 8) # 统计i的个数,3为从第几个开始,8为第几个结束
print(v5) v6 = test.endswith("G") # 以什么结尾
v7 = test.startswith("C") # 以什么开始
print(v6, v7) v8 = test.find("ai", 3, 8) # 从开始往后找,找到第一个后,获取其位置 3,8 表示大于等于3小于8
print(v8) text = "I am {name},age {a}"
print(text) v9 = text.format(name='cuihaicheng', a=24) # 格式化
v10 = text.format_map({'name':'cuihaicheng', 'a':'24'}) # 格式化,字典形式
print(v9)
print(v10) v11 = test.index('i') # index找不到,报错,忽略不用
print(v11) test1 = 'dsjadas234234+'
test2 = 'sdhfasf3249873294'
v12 = test1.isalnum() # 字符串中是否只包含字母和数字
v13 = test2.isalnum()
print(v12)
print(v13) v15 = test2.isalpha() # 只包含字符和汉字
print(v15) test3 = 'username\temail\tpassword\ncuiahicheng\t132@qq.com\tHuawei12#$\ncuiahicheng\t132@qq.com\tHuawei12#$\ncuiahicheng\t132@qq.com\tHuawei12#$\n'
v14 = test3.expandtabs(20)
print(v14) test4 = '②'
v16 = test4.isdecimal() # 主要用它
v17 = test4.isdigit() # 俩个都是判断是否是数字,这个更厉害
v19 = test4.isnumeric() # 是否是数字,支持中文数字
print(v16, v17, v19) a = "_123"
a1 = "def"
v18 = a1.isidentifier() # 字母,数字,下划线 : 标识符 def class
print(v18) test5 = 'sdjfoiasjdf\ndsaf'
v20 = test5.isprintable() # 是否有不可见的字符,制表符,换行
print(v20) test6 = ' '
v21 = test6.isspace() # 判断是否全部为空格
print(v21) test7 = 'Tsdsa Hsdfs Jjsdfjsodf and is dog'
v23 = test7.title() # 转化为标题
print("v23:", v23)
v22 = test7.istitle() # 是否为标题
print("v22:",v22) test8 = '你是风儿我是沙儿'
print(test8)
t = ' '
v24 = t.join(test8) # 将字符串中的每一个元素按照指定分隔符进行拼接(重要)
print(v24) test9 = 'asdsaD'
v28 = test9.islower() # 是否全部是小写
v29 = test9.lower() # 变成小写
print(v28, v29) v30 = test9.isupper() # 是否全部是大写
v31 = test9.upper() # 变成大写
print(v30, v31 ) test10 = ' cuihaicheng '
v32 = test10.lstrip()
v33 = test10.rstrip()
v34 = test10.strip() # 处理空白,\t,\n,字符中的元素(“x”)
print(v32)
print(v33)
print(v34) test11 = 'abcdefghijklmnopqrstuvwxyz'
m = str.maketrans('asdfghjkl', '123456789') # 定义对应关系
new_v = test11.translate(m) # 按照对应关系进行替换
print(new_v) test12 = 'test is a good action'
v35 = test12.partition('o') # 从前开始将字符串分割成三份
print(v35)
v36 = test12.rpartition('o') # 从后开始将字符串分割成三份
print(v36)
v37 = test12.split('o', 1) # 从前开始将字符串分割,不带分隔符,任意指定分成数
print(v37)
v38 = test12.rsplit('o', 2) # 从前开始将字符串分割,不带分隔符,任意指定分成数
print(v38) test13 = 'sadasjd\nasdff\nsadd\nasfadfsdf'
v39 = test13.splitlines(True) # 分割,只能根据true,false:是否保留换行符
print(v39) test14 = 'chdsow'
v40 = test14.startswith('a') # 是否以某个字符开始
v41 = test14.endswith('w') # 是否以某个字符结束
print(v40, v41) test15 = "wqeJIJOomk"
v42 = test15.swapcase() # 大小写转换
print(v42) test16 = 'cuicuicuicuicui'
v43 = test16.replace("ui", "aa", 3) # 替换字符,数字为前几个
print(v43) """
经常使用的有7个
join 将字符串中的每一个元素按照指定分隔符进行拼接
split 从前开始将字符串分割,不带分隔符,任意指定分成数
find 从开始往后找,找到第一个后,获取其位置 3,8 表示大于等于3小于8
strip 处理空白,\t,\n,字符中的元素(“x”)
upper 变成大写
lower 变成小写
replace 替换字符,数字为前几个
""" # 字符串的灰魔法(其他数据类型都可以用) str1 = "你是风儿我是沙儿,缠缠绵绵到天涯"
s1 = str1[2] # 1、索引,下标,获取字符串中的某个字符
s2 = str1[0:4] # 2、切片,获取0=<,<4
s3 = len(str1) # 3、len获取当前字符有几个字符组成,python 2中一个字符3个字节
print(s1, s2, s3) count = 0
while count < len(str1):
print(str1[count])
count += 1
print("========end========") for chc in str1: # 4、for循环
print(chc) ra = range(0, 20, 5) # 5、列出0-20,以5为步长
for it in ra:
print(it) # 将文件对应的索引打印出来 # test17 = input("输入内容>>>")
# print(test17)
# le = len(test17)
# print("len is :")
# r = range(0, le)
# for item in r:
# print(item, "\t", test17[item])
test17 = input("输入内容>>>")
for item in range(0, len(test17)):
print(item, "\t", test17[item])

  

python第二天---字符串的魔法的更多相关文章

  1. 【原创】Python第二章——字符串

    字符串是一个字符序列,(提醒:序列是Python的一个重要的关键词),其中存放UNICODE字符.Python中的字符串是不可变的(immutable),即对字符串执行操作时,总是产生一个新的字符串而 ...

  2. python入门之字符串的魔法

    1.test="alex" v=test.capitalize() print(v)               //输出结果首字母大写 2.test1="alex&qu ...

  3. Python第二天 变量 运算符与表达式 input()与raw_input()区别 字符编码 python转义符 字符串格式化 format函数字符串格式化 帮助

    Python第二天  变量  运算符与表达式  input()与raw_input()区别  字符编码  python转义符  字符串格式化  format函数字符串格式化  帮助 目录 Pychar ...

  4. Python学习-第二天-字符串和常用数据结构

    Python学习-第二天-字符串和常用数据结构 字符串的基本操作 def main(): str1 = 'hello, world!' # 通过len函数计算字符串的长度 print(len(str1 ...

  5. 初识python第二天(3)

    我们接着上一篇博客,继续来来了解Python一些常见类的函数使用方法 一.int # 运算符,>=,比较self是否大于等于value,只要满足大于或者等于其中一个条件,就返回True,否则就返 ...

  6. Python基础(二) —— 字符串、列表、字典等常用操作

    一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. 二.三元运算 result = 值1 if 条件 else 值2 如果条件为真:result = 值1如果条件为 ...

  7. Python补充05 字符串格式化 (%操作符)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在许多编程语言中都包含有格式化字符串的功能,比如C和Fortran语言中的格式化输 ...

  8. Python中的字符串处理

    Python转义字符 在需要在字符中使用特殊字符时,python用反斜杠(\)转义字符.如下表: 转义字符 描述 \(在行尾时) 续行符 \\ 反斜杠符号 \' 单引号 \" 双引号 \a ...

  9. python学习之字符串

    最近在学习python,随手做些记录,方便以后回顾 #字符串是不可再改变的序列aa='abcd'#aa[2:]='ff' #报错,不可直接赋值#字符串格式化:使用格式化操作符即百分号%来实现print ...

随机推荐

  1. python 设计模式之装饰器模式 Decorator Pattern

    #写在前面 已经有一个礼拜多没写博客了,因为沉醉在了<妙味>这部小说里,里面讲的是一个厨师苏秒的故事.现实中大部分人不会有她的天分.我喜欢她的性格:总是想着去解决问题,好像从来没有怨天尤人 ...

  2. gpload导入常见问题汇总

    gpload导入常见问题汇总 java写文件后使用gpload命令导入greenplum: 问题一: 报错信息:invalid byte sequence for encoding "UTF ...

  3. leetcode 384. Shuffle an Array

    384. Shuffle an Array c++ random函数:https://www.jb51.net/article/124108.htm rand()不需要参数,它会返回一个从0到最大随机 ...

  4. un-资源-开源-WebGallery:Windows Web App Gallery

    ylbtech-资源-开源-WebGallery:Windows Web App Gallery Windows Web App Gallery 1.返回顶部   2.返回顶部   3.返回顶部   ...

  5. Flutter BottomNavigationBar 组件

    BottomNavigationBar 是底部导航条,可以让我们定义底部 Tab 切换,bottomNavigationBar是 Scaffold 组件的参数. BottomNavigationBar ...

  6. Qt QLabel加载图片

    QLabel加载图片 //在对应的控件中显示图片 void qm_img::DisplayImg(cv::Mat imgParam, QLabel *labelParam) { if (!imgPar ...

  7. 全面系统Python3入门+进阶_汇总

    https://coding.imooc.com/class/136.html#Anchor 全面系统Python3入门+进阶-1-1 导学 全面系统Python3入门+进阶-1-2 Python的特 ...

  8. Qt编写气体安全管理系统3-用户模块

    一.前言 从这篇开始逐个写具体的功能模块,用户模块主要有四个方面,用户登录.用户退出.用户管理.权限控制.这里都按照简单的常规做法来做,比如用户登录界面,就将用户名提供下拉框选择,然后输入密码,密码框 ...

  9. 【437】Binary search algorithm,二分搜索算法

    Complexity: O(log(n)) Ref: Binary search algorithm or 二分搜索算法 Ref: C 版本 while 循环 C Language scripts b ...

  10. Spring Boot与MyBatis的集成

    SSM(Spring+Spring MVC+MyBatis)是当前主流的框架组合开发方式之一,普遍被应用于互联网项目中.如果要使用Spring Boot开发一个基于SSM框架的应用,那么我们要怎么做呢 ...