capitalize() 把字符串的第一个字符改为大写str1 = 'xiaoxie'str1.capitalize()→Xiaoxiecasefold() 把整个字符串的所有字符改为小写>>> str1 = 'ASSDFAWEadfaAjjIlOOOMMkl'>>> str1.casefold()'assdfaweadfaajjilooommkl'center(width) 将字符串居中,并使用空格 填充至长度width的新字符串>>> str1 = 'asdfaef'>>> str1.center(15)' asdfaef 'count(sub,[,start[,end]]) 返回sub在字符串里边出现的次数,start和end参数表示...
生成字符串变量str=‘python String function‘ 字符串长度获取:len(str)例:print ‘%s length=%d‘ % (str,len(str))连接字符串sStr1 = ‘strcat‘sStr2 = ‘append‘sStr1 += sStr2print sStr1复制字符串#strcpy(sStr1,sStr2)sStr1 = ‘strcpy‘sStr2 = sStr1sStr1 = ‘strcpy2‘print sStr2比較字符串#strcmp(sStr1,sStr2)sStr1 = ‘strchr‘sStr2 = ‘strch‘print cmp(sStr1,sStr2)字符串搜索相关搜索指定字符串,没有返回-...
网址:示例:import base64
import rsadef strkey(strk):b_str = base64.b64decode(strk)if len(b_str) < 162:return Falsehex_str = ‘‘# 按位转换成16进制for x in b_str:h = hex(ord(x))[2:]h = h.rjust(2, ‘0‘)hex_str += h# 找到模数和指数的开头结束位置m_start = 29 * 2e_start = 159 * 2m_len = 128 * 2e_len = 3 * 2modulus = hex_str[m_start:m_start + m_len]exponent = hex_str[e_start:e_start + e_len]return mod...
参考:http://blog.csdn.net/tingsking18/article/details/4033645python内部的字符串是以unicode来编码decode函数用来将其他编码解码为unicodeencode函数将unicode编码为指定的编码类型,例如gbk,utf-8# -*- coding: utf-8 -*-"""
Created on Wed Jan 15 15:20:59 2014@author: hp
"""import urllib2
import re
import time
import jiebaurl="http://blog.sina.com.cn/s/blog_608e1afd0102e5ym.html"def geturl(url):html=urllib...
python字符穿的4种表达方式name=‘张三‘ name="张三" name=‘‘‘张三‘‘‘ name="""张三"""字符串的+法,相当于Java的stringbuffer的append,如n1=‘zhangsan‘ n2=‘sb‘ print(n1+n2),结果就是zhanagsansb;字符串的*法,相对于将某个字符串重复appendN次,如n1=‘zhangsan‘ print(n1*2),结果就是zhangsanzhangsan;python数字的基本运算+-*%这些运算跟Java没什么区别,有区别的是”/“,在Java中”/“是整除取整的运...
python 字符串替换可以用2种方法实现:1是用字符串本身的方法。2用正则来替换字符串 下面用个例子来实验下:a = ‘hello word‘我把a字符串里的word替换为python1用字符串本身的replace方法a.replace(‘word‘,‘python‘)输出的结果是hello python2用正则表达式来完成替换:import restrinfo = re.compile(‘word‘)b = strinfo.sub(‘python‘,a)print b输出的结果也是hello python原文:http://www.cnblogs.com/perfei/p/5340814....
Python 语言使用反斜杠(\)作为转义符,对一些字符进行转义(escape),例如 "\n" "\r\n" 等。所以当 Python 字符串中如果出现反斜杠,则会自动转义其后的字符。但这会导致一个问题,就是,如果只是把反斜杠作为字符字面(liberal)意义,应该如何处理?如果不使用 re 模块(regular expression module),在 Python 字符串中,使用两个反斜杠转义,即可表示一个反斜杠。示例代码如下:import sys # backslash escape
enter = "\r...
整型--int定义:用于比较和计算python2和python3:python2:python2中油int(整型)和long(长整型):1231312L+进制转换:十进制转二进制:正除2,获取余数,余数从上往下进行整合# 123456(十进制)
# 余数
# 3 1
# 1 1
# 0
# bin 求数字的二进制
# 0b -- 二进制
# print(bin(3))二进制转十进制# 1001
# 从右向左,计算机从0开始计数
# 自己手动计算
# 1 * 2 ** 0 + 0 * 2 ** 1 + 0 * 2**2 + 1 * 2** 3
# 1 + 0 + 0 + 8 = 9
# 1是...
python版本 3.5#Author by Liguangbo#_*_ coding:utf-8 _*_str="i like study python, welcome to my python program\t."#首字母大写print(str.capitalize())#I like study python, welcome to my python program.#关键字在字符串中出现的次数print(str.count(" "))#8#打印100个字符,如果str不够,则用-代替,且字符str位于中间print(‘hello world‘.center(20,‘-‘))#----hello world-----#判断字符串是否以‘l’和‘.’开头结...
最近做一个socket server,需要接收组播报文,并进行分析处理。其中涉及的一个问题是,待发送的报文是字符串形式,类似“hello world”。从wireshark截取的报文看,都是16进制数据,以为必须转为该种类型才能发送,需要转换为16进制字符串,类似“0x\a00x\c30x\b4”等。但后来发现,直接发送数据也是ok的,应该是数据发送时自己会进行转码。不了解的时候,网上查了下,发现大家推荐用到的模块是binascii,查看help几个方法如下:FU...
1#coding:utf8 2import demjson3‘‘‘这个方法可以很方便的把dict,list等json格式的数据4 编码成字符串5 和把字符串解码还原为json6 7 注意点:就是字符串是json形式的,但是类型是字符串8‘‘‘ 9 data = [ { ‘a‘ : 1, ‘b‘ : 2, ‘c‘ : 3, ‘d‘ : 4, ‘e‘ : 5 } ]
10 json = demjson.encode(data)
11 data2 = demjson.decode(json)
1213print type(data)
14print type(json)
15print type(data2)
16print json
17prin...
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方法,将不是元音的字...
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里面如何获取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...
add by zhj: 对于多行字符串连接,第6种连接方法很方便,连接时不会添加额外的空格。原文:http://www.cnblogs.com/bigtreei/p/7892113.html1. 加号第一种,有编程经验的人,估计都知道很多语言里面是用加号连接两个字符串,Python里面也是如此直接用 “+” 来连接两个字符串;print ‘Python‘ + ‘Tab‘结果:PythonTab回到顶部2. 逗号第二种比较特殊,使用逗号连接两个字符串,如果两个字符串用“逗号”隔开,那么这两个字符串将...