【day14-python之集合函数字符串格式化】教程文章相关的互联网学习教程文章

Python Codecademy 练习:去掉字符串中的元音字母【代码】

1def anti_vowel(text):2 out=[]3 mystring=list(text)4for i in mystring:5if i notin ["a","e","i","o","u","A","E","I","O","U"]:6 out.append(i)7print("".join(out))8 910 testing=input("请输入字符串:") 11 anti_vowel(testing) 第一次使用remove方法,直接删除list中的元音字母,但是调试时发现去除字母后,list元素的位置发生变化,再次遍历的时候可能会漏掉,于是使用append方法,将不是元音的字...

python 之 字符串的常用方法【代码】【图】

split()函数:返回以指定的字符将字符串分割成多个元素的列表 1 my_str = ‘name is wangxiaoming‘ 2 3print(my_str.split()) #默认不写参数表示按空格符进行切割 4print(my_str.split(‘‘,1)) #数字1 表示切割一次 5 6""" 7运行结果8 9[‘name‘, ‘is‘, ‘wangxiaoming‘] 10[‘name‘, ‘is wangxiaoming‘]my_str = ‘name★is★wangxiaoming‘print(my_str.split(‘★‘)) #默认不写参数表示按空格符进行切割print(my_st...

python连接mysql获取数据 字符串 获取变量【代码】

python脚本中的变量经常会变动,所以考虑写到mysql里面如何获取mysql里面数据作为参数,参考如下脚本: #!/usr/bin/python # -*- coding: utf-8 -*- import MySQLdb # 打开数据库连接 db = MySQLdb.connect("3.12.5.1", "root", "root", "test", charset=‘utf8‘) # 使用cursor()方法获取操作游标 cursor = db.cursor() # 使用execute方法执行sql语句 cursor.execute("select media_source_dir from app_configs a where a.ip_ad...

Python字符串拼接的6种方法(转)【代码】

add by zhj: 对于多行字符串连接,第6种连接方法很方便,连接时不会添加额外的空格。原文:http://www.cnblogs.com/bigtreei/p/7892113.html1. 加号第一种,有编程经验的人,估计都知道很多语言里面是用加号连接两个字符串,Python里面也是如此直接用 “+” 来连接两个字符串;print ‘Python‘ + ‘Tab‘结果:PythonTab回到顶部2. 逗号第二种比较特殊,使用逗号连接两个字符串,如果两个字符串用“逗号”隔开,那么这两个字符串将...

[Python] 字符串拼接方法大 PK,及其要点总结【代码】

代码name = ‘中国人‘ num = 3print(‘我是‘ + name + ",今年有" + str(num) + "个愿望") print(‘我是%s,今年有%d个愿望‘%(name, num)) print(‘我是{name},今年有{num}个愿望‘.format(num=num,name=name)) print(‘我是{},今年有{}个愿望‘.format(name, num)) print(‘我是‘, name, ‘,今年有‘, num, ‘个愿望‘, sep = ‘‘) print(f‘我是{name},今年有{num}个愿望‘) 执行结果我是中国人,今年有3个愿望 我是中国...

python 字符串格式化—format【代码】

Python2.6 开始,新增了一种格式化字符串的函数 str.format()。使用起来简单方便,不会遇到使用%时候格式的选择问题。按照参数默认顺序>>> "yesday is {}, today is {}".format("saturday", "sunday") ‘yesday is saturday, today is sunday‘ >>>指定参数顺序>>> "yesday is {0}, today is {1}, good day is {0}".format("saturday", "sunday") ‘yesday is saturday, today is sunday, good day is saturday‘ >>>指定参数名称#...

Python 字符串,列表,字典,集合的常用方法【代码】

Python 字符串,列表,字典,集合的常用方法String 字符串常用方法# find 方法 查询字符串,返回子串所在的位置的最左段的索引# a = ‘asdfghjkl‘ # print(‘find 方法‘) # print(a.find(‘asd‘)) # print(a.find(‘dfg‘, 5, 10)) # print(‘-‘*50)# join 方法是非常重要的字符串方法, 属于split方法的逆方法。用来连接序列中的元素,元素皆为字符串# b = [‘1‘, ‘2‘, ‘3‘, ‘4‘] # print(‘join 方法‘) # print(‘...

Python 论字符串不变性

>>>‘abc‘ + ‘def‘‘abcdef‘Python分别为‘abc‘和‘def‘分配了空间,当进行连接操作时,Python自动为新的字符串‘abcdef‘分配了空间 >>>s = ‘abc‘>>>s = s + ‘def‘>>>s‘abcdef‘ 上面的例子中,看上去我们把‘abc‘赋给了s,然后在s的末尾添加了‘def‘。这样看起来字符串似乎是可变的其实不然,在"s +‘def‘"这个操作的时候,新建了一个新字符串,然后这个新的对象被赋给了s,原来的字符串‘abc‘被释放掉了我们可以...

Python基础(二)之数据类型和运算(2)——字符串【代码】

字符串基础Python 也提供了可以通过几种不同方式表示的字符串。它们可以用单引号 (‘...‘) 或双引号 ("...") 标识 。\ 可以用来转义引号: >>> ‘spam eggs‘# single quotes‘spam eggs‘ >>> ‘doesn\‘t‘# use \‘ to escape the single quote..."doesn‘t" >>> "doesn‘t"# ...or use double quotes instead"doesn‘t" >>> ‘"Yes," he said.‘‘"Yes," he said.‘ >>> "\"Yes,\" he said."‘"Yes," he said.‘ >>> ‘"Isn\‘...

Python中字符串颜色【代码】

格式:\033[显示方式;前景色;背景色m说明: 前景色 背景色 颜色 --------------------------------------- 30 40 黑色 31 41 红色 32 42 绿色 33 43 黄色 34 44 蓝色 35 45 紫红色 36 46 青蓝色 37...

Python学习系列(三)(字符串)【代码】【图】

Python学习系列(三)(字符串) 一个月没有更新博客了,最近工作上有点小忙,实在是没有坚持住,丢久又有感觉写的必要了,可见本人的坚持精神不佳,本系列没有任何目的,纯属业余学习,或者说是一时兴趣所致。通过本文,能够学习字符串的基本操作,日积月累,多多练习,学到了,会用了才是王道。一、基本概念1,关于转义问题1)“‘‘”方式:>>> s="Hello ‘Jack‘……" >>> print s Hello ‘Jack‘……2)\转义字符:>>> ...

python字符串的魔法7【代码】

#6个基本方法,要记住的join split find strip upper lower#索引,下标:通过索引获取字符串中的某一个字符 test = ‘alex‘ v1 = test[0] v2 = test[1] v3 = test[2] v4 = test[3] v5 = test[0:1] #索引范围, 大于等于0,小于1 v6 = test[0:-1] print(v1) print(v2) print(v3) print(v4) print(v5) print(v6)# len() 字符串里面有多少个字符组成 v7 = len(test) print(v7)# len() 对于列表的使用,计算列表中有多少个元素 li = [11,...

python 将字符串类型转为时间类型【代码】

import datetime,time stringDate = "2006-05-18 19:35:00" dt = datetime.datetime.fromtimestamp(time.mktime(time.strptime(stringDate,"%Y-%m-%d %H:%M:%S"))) print dt 2006-05-18 19:35:00 >>> print type(dt) <type ‘datetime.datetime‘> >>> http://www.sharejs.com/codes/python/835原文:http://www.cnblogs.com/linbinqiang/p/4919323.html

Python3用gevent写个文件字符串查找器【代码】

[本文出自天外归云的博客园]1、递归遍历目录下所有文件并通过finder函数定位指定格式字符串2、用来查找字符串的finder函数是自己定义的,这里定义了一个ip_port_finder通过正则表达式查找ip:port格式(粗匹配:数字.数字.数字.数字:数字)的字符串3、用gevent来实现协程并发完成耗时任务代码如下:# -*- coding: utf-8 -*-import re from os.path import join from os import walk from gevent import monkey import geventmonkey....

【python】字符串

>>> str1="welcom to China">>> str1[2:4]‘lc‘>>> str1[7]‘t‘>>> str1=str1[:8]+"中国"+str1[8:]>>> str1‘welcom t中国o China‘>>> #本质上是重新创建了一个字符串并贴上str1的标签,原来被str1标签贴的字符串仍存在只是现在未被任何标签贴上,之后会被python自动回收机制收回。>>> str2=‘china‘>>> str2.capitalize()‘China‘>>> str2=‘CHINA is beautiful, HAHA‘>>> str2.casefold()‘china is beautiful, haha‘>>> ...