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——字符串规则的更多相关文章

  1. 【python基础语法】字符串常用方法 、列表(第3天课堂笔记)

    """ 字符串的方法 join 字符串拼接,将列表转换为字符串 find 查找元素位置 count 查找元素个数 replace 替换字符 split 字符串分割,将字符 ...

  2. python基础语法_字符串编码

    Python常用字符编码 http://www.cnblogs.com/schut/p/8406897.html   Python常见字符编码间的转换   在字符串写入文件时,有时会因编码问题导致无法 ...

  3. python基础语法之字符串

    1 字符串中*的使用 *可以使字符串重复n次 print('hello world ' * 2) # hello world hello world 2 索引获取字符串的字符元素 print('hel ...

  4. python基础语法及知识点总结

    本文转载于星过无痕的博客http://www.cnblogs.com/linxiangpeng/p/6403991.html 在此表达对原创作者的感激之情,多谢星过无痕的分享!谢谢! Python学习 ...

  5. Python基础语法题库

    引言: 语法练习包括Python基础语法.数据类型.字符编码和简单文件操作等内容. 正文(参考答案附录在题目下方): 1.Python 里用来告知解释器跳过当前循环中的剩余语句,然后继续进行下一轮循环 ...

  6. python之最强王者(2)——python基础语法

    背景介绍:由于本人一直做java开发,也是从txt开始写hello,world,使用javac命令编译,一直到使用myeclipse,其中的道理和辛酸都懂(请容许我擦干眼角的泪水),所以对于pytho ...

  7. Python 基础语法(四)

    Python 基础语法(四) --------------------------------------------接 Python 基础语法(三)------------------------- ...

  8. Python 基础语法(二)

    Python 基础语法(二) --------------------------------------------接 Python 基础语法(一) ------------------------ ...

  9. Python 基础语法

    Python 基础语法 Python语言与Perl,C和Java等语言有许多相似之处.但是,也存在一些差异. 第一个Python程序 E:\Python>python Python 3.3.5 ...

随机推荐

  1. C/C++,被誉为“最经典的编程语言”,不仅是因为编程入门需要学?

    计算机诞生初期,用机器语言或汇编语言编写程序; 第一种高级语言FORTRAN诞生于1954年; BASIC语言(1964)是由FORTRAN语言的简化而成的是为初学者设计的小型高级语言; C语言是19 ...

  2. 一图了解redis

    了解redis,这一张图就够了,话不多说,看图: 版权所有,转载请注明出处,欢迎讨论交流

  3. Redis持久化存储(三)

    redis高级特性-发布订阅消息服务功能 Pub/Sub 订阅,取消订阅和发布实现了发布/订阅消息范式(引自wikipedia),发送者(发布者)不是计划发送消息给特定的接收者(订阅者).而是发布的消 ...

  4. Spring5参考指南:组件扫描

    文章目录 组件扫描 @Component 元注解和组合注解 组件内部定义Bean元数据 为自动检测组件命名 为自动检测的组件提供作用域 生成候选组件的索引 组件扫描 上一篇文章我们讲到了annotat ...

  5. Spring boot自定义parent POM

    文章目录 概述 不使用Parent POM来引入Spring boot 覆盖依赖项版本 概述 在之前的Spring Boot例子中,我们都会用到这样的parent POM. <parent> ...

  6. Python下redis包安装

    找到Python的第三方包安装路径,在dos命令行中切换到该目录,输入: pip install redis 最后在Python解释器中即可.

  7. Java算法之 二分搜寻法 ( 搜寻原则的代表)

    为什么80%的码农都做不了架构师?>>>   二分搜寻法 ( 搜寻原则的代表) 1.二分查找又称折半查找,它是一种效率较高的查找方法. 2.二分查找要求:(1)必须采用顺序存储结构 ...

  8. 获取系统DPI、系统显示比例等

    using System; using System.Drawing; using System.Runtime.InteropServices; namespace XYDES { public c ...

  9. Codeforces Round #587

    题目链接:Round #587 题目答案:官方Editorial.My Solution A. Prefixes 题意:给一字符串,只含有'a'或'b',需要改变某些位置('a'变'b'或'b'变'a ...

  10. 数学--数论--随机算法--Pollard Rho 大数分解算法(纯模板带输出)

    ACM常用模板合集 #include <bits/stdc++.h> using namespace std; typedef long long ll; ll pr; ll pmod(l ...