徐州哪有好的Python培训学校

达内致力于面向IT互联网行业,培养软件开发工程师、测试工程师、系统管理员、智能硬件工程师、UI设计师、网络营销工程师、会计等职场人才。2015年起,推出面向青少年的少儿编程、智能机器人编程、编程数学等K12课程


Python中字符串的处理技巧有哪些

空格剥离

空格剥离是字符串处理的一种基本操作,可以使用lstrip()方法(左)剥离前导空格,使用rstrip()(右)方法对尾随空格进行剥离,以及使用strip()剥离前导和尾随空格。

s = ' This is a sentence with whitespace. \n'

print('Strip leading whitespace: {}'.format(s.lstrip()))print('Strip trailing whitespace: {}'.format(s.rstrip()))print('Strip all whitespace: {}'.format(s.strip()))Strip leading whitespace: This is a sentence with whitespace.

Strip trailing whitespace: This is a sentence with whitespace.Strip all whitespace: This is a sentence with whitespace.

对剥离除空格以外的字符感兴趣吗?同样的方法也很有用,可以通过传递想要剥离的字符来剥离字符。

s = 'This is a sentence with unwanted characters.AAAAAAAA'

print('Strip unwanted characters: {}'.format(s.rstrip('A')))

Strip unwanted characters: This is a sentence with unwanted characters.

必要时不要忘记检查字符串 format()文档。

format()文档:https://docs.python.org/3/library/stdtypes.html#str.format

领取试听课
每天限量名额,先到先得
温馨提示:为不影响您的学业,来 徐州python培训 校区前请先电话或QQ咨询,方便我校安排相关的专业老师为您解答
  • 详情请进入 达内IT培训江苏校区

关于我们 | 招生信息 | 新闻中心 | 学校动态

版权所有:搜学搜课(www.soxsok.com)