• string
A string is a data type that all systems support string
Windows,linux,nginx
Java c C + + C #
1. Create a string
s=‘Hello World‘s="Hello World"
2. Support Shard operation
s=‘Hello World‘print sprint s[0:5]# 返回Helloprint s[:5] # 返回Hello
3, no characters, ' H ', "H" are the same, no character and string difference is a string
Accessing a single character
print s[1] #e 同列表一样
4. String cannot be modified
s1=s[0:5]s2=s[5:11]s3=s1+‘PYthon‘+s2
5. Operator:
拼接操作符:"+"重复操作符:"*"关系操作符:< ><= >=逻辑操作符: and or not
• Common methods
Help ()
1, Captalize () returns the string after the first character is capitalized
s=‘hello‘print s.capitalize() #Hello
2, Casefold () return string all lowercase string returns #python3 version
3, center (width) Center (width: Fill the number of spaces)
s=‘hello‘print s.center(20)#hello居中在width数量空格
4, COUNT (sub): Returns the number of strings that appear in a sub
s=‘hello‘print s.count(‘l‘) #返回2count(sub,start,end)s=‘hello world‘print s.count(‘o‘,6,11) #返回1print s.count(‘o‘,4,11)#返回2
5, Encode (encoding= ' UTF-8 ')
以指定的编码格式对字符串进行编码
6, Endswidth (str)
Returns a Boolean value ending in str
s=‘hello‘print s.endswidth(‘a‘)#Falseprint s.endswidth(‘o‘)#True
7|, Expandtabs (tablesize)
Converts the tab in a string to a space (default tablesize=8)
\ t escape to tab (length depends on tablesize)
s=‘Hello\tWorld‘print sprint s.expandtabs(16) #Hello 有16 个空格 World
8. Find (sub)
Finds the index value of the first occurrence of a sub in a string and cannot find the return-1
s=‘hello‘print s.find(‘e‘)#返回 1 下标print s.find(‘v‘)#返回-1 没有这个字print s.find(‘a‘,3,5)#从3下标到5 下标出现a的下标
9, Isalum (sub):
Returns true if all characters of the string are letters or numbers, otherwise false
10, Isalpha ():
Determine if the character
·格式化字符串
Output strings in a uniform format
Format ()
1. Pass a replacement field {} to indicate a field
‘{0} love {1}‘.format(‘I‘,‘Baidu‘)#‘I love Baidu‘用{}表示字段,数字表示位置,format方法对传进来参数进行格式化处理
2. Use the letter to represent the position
‘{a} love {b}‘.format(a=‘I‘,b=‘Baidu‘)#‘I love Baidu‘
3, if you want to use the letter to represent the position, but also want to use the number to represent the position ()
‘{0} love {b}‘.format(‘I‘,b=‘Baidu‘)#‘I love Baidu注意:位置参数必须在关键字参数在前,第一个关键字定义后,编译器认为后面的参数都是按照关键字参数‘{a} love {0}‘.format(a=‘I‘,‘Baidu‘)#返回错误
4. print ' {} ' in string
All {} must receive the parameter, and if there is a need to hit {}, you need to escape
print ' {{0}} '. Format (' hehe ') # ' {hehe} '
‘{{{0}}} love {1}‘.format(‘I‘,‘Baidu‘)#‘{I} love Baidu‘
• Operator of the string
1.%c: Format character and ASCII code
print(‘%c‘ %65)#返回A print(‘%c‘ %97)#返回a
2.%s: Formatting a string
print(‘%s Love Baidu‘ %‘I‘ )#I Love Baidu
3,%d: formatted integer
print(‘%d‘%100) #100
4.%o: Formatting unsigned octal numbers
print (‘%o‘%10)#十进制转八进制 12
5.%x: Format unsigned hexadecimal number
6.%x: Format unsigned hexadecimal number (uppercase)
7,%f: Format fixed-point number (floating-point), you can specify the precision after the decimal
Do not specify a default 6-bit decimal
print (‘%f‘ %3.14) #3.1400008、%e:利用科学计数法格式化定点数(浮点数) print (‘%e‘ %378.1415926)#3.781416e+029、%E同上只是大小写不同10、%g:根据值的大小,自动选择使用%e还是%f11|%G:同上只是大小写不同
Python---008--string