Python 字符串,变量
2017-12-21 00:00
204 查看
字符串,变量
基本运算
+ - * / ( )
print 'string' + 9 // 字符串和变量不能一起
print 'string' * 9 // 输出 9个string
字符串数组
name = 'hello' name[0] = 'h' name[-2] = 'l'
获取子字符串 name[1:3] 不包含最后一个 [start : end]
find函数
<string>.find(<string>)第一次出现的index 未找到返回 -1
's'.find('s') = 0
's'.find('') = 0
<string>.find(<string>,<number>)从 number未知2开始查找
str 函数
将number转变成string练习
四舍五入如果一个小数 加上0.5后,整数还是不进位,则取整数,进位还是取整数
x = 3.14159 x =x+0.5 num = str(x).find('.') x = str(x) print (x[:num])
替换
find找到要替换的index
len(str)获取长度
原有字符[:index]+ replace + 原有字符[index+len:]
判断是否是回文
使用 string[::-1] 来使string字符串倒写然后通过 find函数 判断是否相同 ,返回 1 和 0
word = "madam" is_palindrome = word.find(word[::-1]) print is_palindrome
相关文章推荐
- 复习1次课(11月28日) 1.3 python的文件类型 1.4 python的变量 1.5 python的数值和字符串 1.6 元组 1.7 列表 1.8 字典 1.9 字典练习
- Python 入门笔记 字符串 变量 标示符
- python | 变量与数据 | 字符串转义 | 多行输入
- Python基础2注释+变量+字符串
- Python之变量和字符串(1)
- python变量中的数据(一),数字和字符串
- python 变量,数组,字符串
- Python基本元素:数字、字符串和变量
- Python第二天 变量 运算符与表达式 input()与raw_input()区别 字符编码 python转义符 字符串格式化 format函数字符串格式化 帮助
- Python:如何将字符串作为变量名
- Python笔记 - 变量和字符串
- Python入门之变量与字符串
- Python字符串String和变量 Variable
- 复习贴-python的变量,数值和字符串
- python如何import 文件名存在字符串变量里的module
- Python:变量与字符串
- Python:字符串中引用外部变量的3种方法
- python基础学习之变量和字符串
- python如何将字符串作为变量
- python教程1:Python基础之数据类型和变量、字符串和编码