python基础内容整理(一)
基本数据类型 字符串 String 字符串是不可变类型 字符串的分割: s.split(sep)以给定的sep为分隔符对s进行分割。 In [4]: s = "hello world" s.split() Out[4]: ['hello', 'world'] In [11]: line = "1,2,3,4,5" numbers = line.split(',') 连接 与分割相反,s.join(str_sequence)的作用是以s为连接符将字符串序列str_sequence中的元素连接起来,并返回连接后得到的新字符串: In [13]: s = ',' s.join(numbers) Out[13]: '1,2,3,4,5' 替换 s.replace(part1, part2)将字符串s中指定的部分part1替换成想要的部分part2,并返回新的字符串。 每个部分都会替换 不止是第一个 s的值本身没有发生变化 只是生成一个新的字符串 In [17]: s = "hello world" s.replace('l', ' python ') Out[17]: 'he python python o wor python d' In [19]: s Out[19]: 'hello world' 大小写转换 s.upper()方法返回一个将s中的字母全部大写的新字符串。 s