先举一个例子:
# coding: utf-8
string = 'When your mind is simple, your world is simple'
print('定义长度:', string.zfill(50))
print('获取元素i的个数:', string.count('i', 0, 50))
print('判断开头的元素是不是e:',string.startswith('e'))
print('判断结尾的元素是不是e:', string.endswith('e'))
print('请找出元素r在哪个位置:', string.find('r'))
print('请找出元素a在哪个位置:', string.find('a'))
print('请把字符串中的元素W去掉:', string.strip('W'))
print('请把字符串中的your改成my:', string.replace('your', 'my'))
print('判断字符串是不是由空格组成:', string.isspace())
print('请判断字符串是不是标题:', string.istitle())
print('请判断字符串的字母是不是都是大写:', string.isupper())
print('请判断字符串中的字母是不是都是小写:', string.islower())
输出结果:
定义长度: 0000When your mind is simple, your world is simple
获取元素i的个数: 5
判断开头的元素是不是e: False
判断结尾的元素是不是e: True
请找出元素r在哪个位置: 8
请找出元素a在哪个位置: -1
请把字符串中的元素W去掉: hen your mind is simple, your world is simple
请把字符串中的your改成my: When my mind is simple, my world is simple
判断字符串是不是由空格组成: False
请判断字符串是不是标题: False
请判断字符串的字母是不是都是大写: False
请判断字符串中的字母是不是都是小写: False