Python基础语法day_02——字符串规则
day_02
- 使用方法修改字符串的大小写
将字符串首字母变成大写
>>> name = "ada lovelace"
>>> print(name.title())
Ada Lovelace
将字符串全部变成大写
>>> print(name.upper())
ADA LOVELACE
将字符串全部变成小写
>>> print(name.lower())
ada lovelace
- 合并字符串
使用 + 来合并 first_name,last_name,空格
>>> first_name = "ada"
>>> last_name = "lovelace"
>>> full_name = first_name + " " +last_name
>>> print(full_name)
ada lovelace
乘热打铁,使用title()来组装一个字符串
>>> print("Hello,"+ " " + full_name.title() +"!")
Hello, Ada Lovelace!
也可以将整条信息储存在一个变量中
>>> message = "Hello,"+ " " + full_name.title() +"!"
>>> print(message)
Hello, Ada Lovelace!
- 使用制表符或换行符来添加空白
在字符中添加制表符,可以使用字符组合\t
>>> print("Python")
Python
>>> print("\tPython")
Python
在字符中添加换行符,可以使用字符组合\n
>>> print("I\nlove\nPython")
I
love
Python
使用换行符加上制表符
>>> print("I\n\tlove\n\tPython")
I
love
Python
- 删除空白
使用方法rstrip(),这种方法只是暂时的
>>> a_word = 'python '
>>> a_word
'python ' #输入的时候有一个空格符
>>> a_word.rstrip()
'python' #消除了空白
>>> a_word
'python '
要想永久删除空格,还需将删除的操作重新赋值到原来的变量里面
>>> a_word = a_word.rstrip()
>>> a_word
'python'
rstrip()剔除右边的空白
lstrip()剔除左边的空白
strip()剔除全部空白
>>> a_word = ' python ' #左边和右边都有空白
>>> a_word.rstrip()
' python'
>>> a_word.lstrip()
'python '
>>> a_word.strip()
'python'
- 使用字符串是避免语法错误
撇号位于两个双引号之间,故能识别出整个字符串
>>> message = "I'm a student."
>>> print(message)
I'm a student.
但是使用单引号时
>>> message = 'I'm a student.'
SyntaxError: invalid syntax
Python无法正确地确定字符串结束的位置
- 数字
整数
在Python中可以对整数进行+ - * / 运算
>>> 1+1
2
>>> 3-5
-2
>>> 3-2
1
>>> 3/2
1.5
>>> 8*9
72
Python使用两个乘号(**)进行乘方运算
>>> 8*9
72
>>> 3**3
27
>>> 2**3
8
>>> 5**5
3125
浮点数
使用浮点数是无需考虑其他行为。只需输入要使用的数字
>>> 1.2+5
6.2
>>> 1.2+36.3
37.5
>>> 1.7+9.6
11.299999999999999
>>> 1.6+1.6
3.2
>>> 0.2+0.1
0.30000000000000004
>>> 2*0.2
0.4
>>> 1*0.3
0.3
有时结果包含的小数点是不确定的
但是无需担心,接下来的学习中会解决此类问题
- 使用函数str()避免类型错误
有时候你希望
>>> age = 22
>>> message = "Happy" + " " + age +"rd Birthday!!"
你会希望输出"Happy 22rd Byrthday!!"
但是,很遗憾上述代码会引发错误
>>> age = 22
>>> message = "Happy" + " " + age +"rd Birthday!!"
Traceback (most recent call last):
File "<pyshell#54>", line 1, in <module>
message = "Happy" + " " + age +"rd Birthday!!"
TypeError: can only concatenate str (not "int") to str
这是一个类型错误,Python发现你使用了一个值为整数(int)的值,它不知道如何解读这个值,它认为这个变量表达的可能是22,也可能是字符2和字符2,为此,在调用变量是。你需要调用str()函数来将这个变量变成字符串。
>>>age = 22
>>> message = "Happy" + " " + str(age) + "rd Birthday!!"
>>> print(message)
Happy 22rd Birthday!!
- python之禅
在IDLE中运行import this就可以得到Python之禅
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
以上就是Python第二天的学习内容,希望能对你有所帮助,祝你学有所成!!!
Python基础语法day_02——字符串规则的更多相关文章
- 【python基础语法】字符串常用方法 、列表(第3天课堂笔记)
""" 字符串的方法 join 字符串拼接,将列表转换为字符串 find 查找元素位置 count 查找元素个数 replace 替换字符 split 字符串分割,将字符 ...
- python基础语法_字符串编码
Python常用字符编码 http://www.cnblogs.com/schut/p/8406897.html Python常见字符编码间的转换 在字符串写入文件时,有时会因编码问题导致无法 ...
- python基础语法之字符串
1 字符串中*的使用 *可以使字符串重复n次 print('hello world ' * 2) # hello world hello world 2 索引获取字符串的字符元素 print('hel ...
- python基础语法及知识点总结
本文转载于星过无痕的博客http://www.cnblogs.com/linxiangpeng/p/6403991.html 在此表达对原创作者的感激之情,多谢星过无痕的分享!谢谢! Python学习 ...
- Python基础语法题库
引言: 语法练习包括Python基础语法.数据类型.字符编码和简单文件操作等内容. 正文(参考答案附录在题目下方): 1.Python 里用来告知解释器跳过当前循环中的剩余语句,然后继续进行下一轮循环 ...
- python之最强王者(2)——python基础语法
背景介绍:由于本人一直做java开发,也是从txt开始写hello,world,使用javac命令编译,一直到使用myeclipse,其中的道理和辛酸都懂(请容许我擦干眼角的泪水),所以对于pytho ...
- Python 基础语法(四)
Python 基础语法(四) --------------------------------------------接 Python 基础语法(三)------------------------- ...
- Python 基础语法(二)
Python 基础语法(二) --------------------------------------------接 Python 基础语法(一) ------------------------ ...
- Python 基础语法
Python 基础语法 Python语言与Perl,C和Java等语言有许多相似之处.但是,也存在一些差异. 第一个Python程序 E:\Python>python Python 3.3.5 ...
随机推荐
- SpringCloud(五)学习笔记之Hystrix
在微服务架构中多层服务之间会相互调用,如果其中有一层服务故障了,可能会导致一层服务或者多层服务故障,从而导致整个系统故障.这种现象被称为服务雪崩效应. Hystrix组件就可以解决此类问题,Hystr ...
- Java 多线程 -- 协作模型:生产消费者实现方式一:管程法
多线程通过管程法实现生产消费者模式需要借助中间容器作为换从区,还包括生产者.消费者.下面以蒸馒头为列,写一个demo. 中间容器: 为了防止数据错乱,还需要给生产和消费方法加锁 并且生产者在容器写满的 ...
- sql注入原理+mysql相关知识点
什么是SQL注入 sql就是经常说的数据库,而sql注入就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令.SQL注入是比较常见的网络攻击 ...
- 转:handler.post 为什么要将thread对象post到handler中执行呢?
转载网址:http://blog.csdn.net/fei0724/article/details/8664462在Android中使用Handler和Thread线程执行后台操作 对于线程的控制,我 ...
- thinkphp--多表查询
我们可以将两个表连起来一起查询数据,我现在有两张表,一个是feedback表和member表,如图: 总目录: 上代码: $where = array(); $"; $Model = M(' ...
- CG-CTF(4)
CG-CTF https://cgctf.nuptsast.com/challenges#Web 续上~ 第十六题:bypass again 代码分析: 当a不等于b,且a和b的md5值相同时,才会返 ...
- 2019-2020-1 20199329《Linux内核原理与分析》第十二周作业
<Linux内核原理与分析>第十二周作业 一.本周内容概述: 通过编程理解 Set-UID 的运行机制与安全问题 完成实验楼上的<SET-UID程序漏洞实验> 二.本周学习内容 ...
- Add text to 'Ready Page' in Inno Setup
https://stackoverflow.com/questions/1218411/add-text-to-ready-page-in-inno-setup
- Spring5参考指南:Environment
文章目录 Profiles PropertySource 使用@PropertySource Spring的Environment接口有两个关键的作用:1. Profile, 2.properties ...
- R 语言命令行参数处理
在unix.windows外部需要调用R脚本执行,然后又需要输入不同的参数,类似shell脚本的命令行参数输入,可以使用Rcript命令实现. 命令格式:Rscript [options] [-e e ...