python string

A 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 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 be 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 string in reverse order

def resstr(ch):
    mid=len(ch)
    for c in range(mid):
        print(ch[mid-1-c],end='')
>>> resstr('abcde')
edcba

The preparation of sb, the string is reversed, and the string cannot be modified. Error message: 'str' object does not support item assignment

Fragmentation of a string is to separate a part of a string from a given string, which can be indexed in the following form: i, j, ki
i is the starting position, j is the end position of the index, but does not include the string at the j position , the step size of each increment of the index number is k

>>> s="hello world"
>>> print(s[0:len(s):2])
hlowrd
>>> print(s[1:len(s):3],end='---')
eood---

The index index is from 0 to len(str)-1, and a negative index can also be used, and the range is -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 the string fragment, the start position i of the index, the end position j of the index, and the step size k can be omitted. When i is omitted, it starts from 0 or -1. When j is omitted, it ends at the end of the last string, and k is omitted. The time step is 1.

st='asdfghjk'
print(st[:0:-1])
print(st[2::2])
print(st[0:5:])
kjhgfds
dgj
asdfg

String-related operations can perform 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 changes, a new string will be returned. The 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 methods of strings, not to mention, it is meaningless.

byte object
In Python, a byte is different from a string. A sequence consisting of a series of immutable unicode characters is called a string. A sequence consisting of a series of strings with immutable encodings 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 a 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
Xiamen Forklift Rental Service Company

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324669030&siteId=291194637