1. 换行

print函数默认会换行,但是如果想多次print不换行,那么可以如下使用:

print ("hello",end="")

print("world")

因为print函数默认为print('content',end="\n")  如果我们需要不换行,那么要修改end的默认值。

2. 打印一个空行: print ()  会默认打印一个空行。

3. 利用制表符将打印内容对齐:

print ("abd\txyz")
print ("abcd\txyz")
print ("abcde\txyz")
输出结果为:

   

以上代码会让xyz的打印位置从下一个tab位置开始不管abc*的长度如果变化。除非它的长度超过一个tab的距离,那么xyz会从下一个tab位置开始打印。

4. 插入变量: 使用%
%s 表示插入字符串变量,%i表示插入整个数,%f表示插入浮点数
print("%10.3f"%math.pi) 表示输出的数字占位为10,输出小数点后3位,意味着前面会有6个空格。
print("%4.3s"%"hello")表示输出的占位为4,但是只输出字符串前三位,意味着前面会有一个空格。

5. 更多的字符串处理
- 字符串相加 print("hello"+"world") 会得到helloworld 而 print ("hello","world")会得到 hello world 会默认有一个空格
- 字符串分解 s = "hello,world" s.split(",") 会得到一个list ["hello","world"]
- 字符串连接 ",".join(["hello","world"])会将LIST中的值通过“,”连接成一个字符串
- 搜索字符串 "hello,wolrd".startwith("hello")返回布尔值 类似的还有endwith()
- 在字符串中搜索 in/index 如: "h" in "hello" 返回布尔值,print ("hello".index("o"))返回"o"在前面字符串中的位置。
- 删除字符串的一部分 "hello,world".strip("world")会删除“world”,如果什么都不传给strip,那么会默认删除前面字符串末尾所有的空白格。
- 转变字符的大小写 string.upper() string.lower()






posted on 2018-01-18 14:22  srialy  阅读(140)  评论(0编辑  收藏  举报