python之字符转换_python学习之字符串转换

配置环境:python 3.6   python编辑器:pycharm

代码如下:

#!/usr/bin/env python

#-*- coding: utf-8 -*-

def strcase():

"字符串大小写转换"

print("演示字符串大小写转换")

print("演示字符串s赋值为:' this is a python '")

s = ' this is a python '

print("大写转换成小写:\ts.lower() \t= %s"%(s.lower()))

print("小写转换成大写:\ts.upper() \t= %s"%(s.upper()))

print("大小写转换:\t\ts.swapcase() \t= %s"%(s.swapcase()))

print("首字母大写:\t\ts.title() \t= %s"%(s.title()))

print('\n')

def strfind():

"字符串搜索、替换"

print("演示字符串搜索、替换等")

print("演示字符串s赋值为:' this is a python '")

s = ' this is a python '

print("字符串搜索:\t\ts.find('is') \t= %s"%(s.find('is')))

print("字符串统计:\t\ts.count('s') \t= %s"%(s.count('s')))

print("字符串替换:\t\ts.replace('is','is') = %s"%(s.replace('is','is')))

print("去左右空格:\t\ts.strip() \t=#%s#"%(s.strip()))

print("去左边空格:\t\ts.lstrip() \t=#%s#"%(s.lstrip()))

print("去右边空格:\t\ts.rstrip() \t=#%s#"%(s.rstrip()))

print('\n')

def strsplit():

"字符串分割、组合"

print("演示字符串分割、组合")

print("演示字符串s赋值为:' this is a python '")

s = ' this is a python '

print("字符串分割:\t\ts.split() \t= %s"%(s.split()))

print("字符串组合1: '#'.join(['this','is','a','python']) \t= %s"%('#'.join(['this','is','a','python'])))

print("字符串组合2: '$'.join(['this','is','a','python']) \t= %s"%('$'.join(['this','is','a','python'])))

print("字符串组合3: ' '.join(['this','is','a','python']) \t= %s"%(' '.join(['this','is','a','python'])))

print('\n')

def strtest():

"字符串测试"

print("演示字符串测试")

print("演示字符串s1赋值为:'abcd'")

s1 = 'abcd'

print("测试s.isalpha() = %s"%(s1.isalpha()))

print("测试s.isdigit() = %s"%(s1.isdigit()))

print("测试s.isspace() = %s"%(s1.isspace()))

print("测试s.islower() = %s"%(s1.islower()))

print("测试s.isupper() = %s"%(s1.isupper()))

print("测试s.istitle() = %s"%(s1.istitle()))

if __name__ == '__main__':

strcase()

strfind()

strsplit()

strtest()

希望与广大网友互动??

点此进行留言吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值