Python string is a sequence type whose elements are characters. Because the sequence type is a data structure in which elements are placed sequentially, you can obtain a certain character by index, or specify an index range to obtain a group of characters.
>>> ch='abcde' >>> print("ch[0]=",ch[0],"ch[-1]=",ch[-1]) ch[0]= a ch[-1]= e
index is an integer and cannot go out of bounds, from 0 to lne(str)-1, otherwise an error will occur.
>>> len(ch) 5 >>> ch[5] Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> ch[5] IndexError: string index out of range
Print the string in reverse order
def resstr(ch): mid=len(ch) for c in range(mid): print(ch[mid-1-c],end='') >>> resstr('abcde') edcba
Preparation for sb, if the string is reversed, the string cannot be modified. Error message: 'str' object does not support item assignment
The sharding of a string is to separate part of the string from a given string. You can use the following form to index i, j, k
i is the starting position, j is the end position of the index, but does not include the string at position j. The step size of each increase in the index number is k
>>> s="hello world" >>> print(s[0:len(s):2]) hlowrd >>> print(s[1:len(s):3],end='---') eood---
index index, which is from 0 to len(str) -1, you can also use negative indexes, ranging from -n to -1. The starting position of the negative index is the end of the string.
st='asdfg' print(st[-1:0:-1]) gfds
The index of string fragmentation, the starting position of the index i, the ending position of the index j, and the step size k can be omitted. When i is omitted, it starts from 0 or -1, and when j is omitted it goes to the end. The end of a string. When k is omitted, the step size is 1.
st='asdfghjk' print(st[:0:-1]) print(st[2::2]) print(st[0:5:]) kjhgfds dgj asdfg
String-related operations can include connection operations, logical operations, and string processing functions.
st1='abc' st2="def" print("{0}+{1}={2}".format(st1,st2,st1+st2)) print("{0:s}*5={1}".format(st1,st1*5)) abc+def=abcdef abc*5=abcabcabcabcabc
String methods
Strings are immutable. After any string is changed, a new string will be returned. Python string string can be regarded as a class.
st1='abcDEF' print("{0:s}.upper()={1:s}".format(st1,st1.upper())) print("{0:s}.lower()={1:s}".format(st1,st1.lower())) print("{0:s}.swapcase()={1:s}".format(st1,st1.swapcase())) abcDEF.upper()=ABCDEF abcDEF.lower()=abcdef abcDEF.swapcase()=ABCdef st1='abcDEFasde' print("a count={0}".format(st1.count('a'))) print("{0} start with {1} is {2} ".format(st1,'abc',st1.startswith('abc'))) print("{0} end with {1} is {2} ".format(st1,'de',st1.startswith('de'))) a count=2 abcDEFasde start with abc is True abcDEFasde end with de is False st1=' abcDEFasde' print("{0} replace 123 :{1}".format(st1,st1.replace('abc','123'))) print( "{0} remove {1} left char---{2}".format(st1,' ',st1.strip())) abcDEFasde replace 123 : 123DEFasde abcDEFasde remove left char---abcDEFasde
There are too many string methods, so I won’t go into them anymore, it’s not interesting.
byte object
In Python, byte is different from string. A sequence composed of a series of unchangeable unicode characters is called a string. A sequence consisting of a series of strings with an immutable encoding between 0 and 255 is called a byte object.
by=b'abc &' print(type(by)) print("length=",len(by)) <class 'bytes'> length= 5
Add 'b' in front of the string to define the byte object. Each string can be ascii characters, etc. You can use the len() function to calculate the length of the byte object.
ch=input('输入几个数字逗号隔开:') d=ch.split(',') print(d) sum=0 for num in d: sum+=float(num) print("ths sum=",sum) 输入几个数字逗号隔开:2.2,3.3,5.5,6.8 ['2.2', '3.3', '5.5', '6.8'] ths sum= 17.8
Related recommendations:
##Practical application of python strings
Detailed explanation of various built-in functions of Python3 strings
The above is the detailed content of A brief discussion on python strings. For more information, please follow other related articles on the PHP Chinese website!