代码改变世界

小白成长之路:初识python(一)

2017-10-22 21:03  张小贤TT  阅读(214)  评论(0编辑  收藏  举报

  每日一篇博文,记录小白成长之路

  这几天一直忙着租房子,真是觉得心好累,今天看的不是很多,好了废话不多说  上代码 

 

 

===============================================================================  

# Python中关于字符串类的一些基本函数

# str.upper() 字符串变大写 返回一个新的字符串
# str_1 = "hey"
# str_2 = str_1.upper()
# print (str_2)

# str.lower() 把字符串中的每个字母都变小写

# str.center(width,fillchar =None)
# a1 = "alex" #内容居中,width为设置的总长度
# ret = a1.center(10,'*')
# print (ret)
# ***alex***

# format
# s = "hello i am {0},i am {1} years old"
# print (s)
#在format函数中,{0}{1}充当与占位符的作用
# new_s = s.format('tianqi',23) #'tianqi'代替{0}的位置
# print (new_s)
# hello i am tianqi,i am 23 years old

# str.count(self, sub, start=None, end=None)
# 查看字符串中子序列的个数
# s1 = "hello world"
# print (s1.count('ll'))
# 1

#str.find() 查找子序列的位置 没有返回-1
# s1 = "hello world"
# print (s1.find(('ll')))
# 2

#str.endswith() 是否以xxx结束 返回一个bool值
# str.startswith() 是否以..开始,返回一个布尔值


#str.capitalize() 首字母变大写

# ''.join() 把列表(元组)中的元素拼接成字符串
# li = ['abc','def']
# s = '*'.join(li)
# print (s)
# abc*def

# 字符串左右对齐
# str.ljust()
# str.rjust()


# str.lstrip() 去除左边的空格
# str.rstrip() 去处右边的空格
# str.strip() 去除左右两边的空格

# str.partition('xx') 按照‘xx’分割字符串 返回一个元组
# 从左到右的顺序在字符串中查找‘’中的元素(第一个匹配的),并且把‘’中的元素也放进元组中
# a = 'alex sb alex'
# ret = a.partition('l')
# print (ret)
# ('a', 'l', 'ex sb alex')


# str.replace('xx','xx') 替换字符串

# str.split('')
# 按照字符串中的某一元素分割字符串 返回一个列表
# ‘’中的内容不保留
# a = "alex SB slex"
# ret = a.split("l")
# print (ret)
# ['a', 'ex SB s', 'ex']

# str.swapcase() 大写变小写 小写变大写

# 字符串的每个单词的首字母大写
# str.title()
# a = 'alex sb alex'
# ret = a.title()
# print (ret)
# Alex Sb Alex

#############################################
##########索引 和 切片########################
# len(str) 获取字符串的长度
# str[index] 获取字符串中的某一字符

# 切片 str[:]
# a = 'alex'
# print (a[0:2])
# al