Home  >  Article  >  Backend Development  >  A brief discussion on python strings

A brief discussion on python strings

零到壹度
零到壹度Original
2018-04-23 11:30:311578browse

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=&#39;&#39;)
>>> resstr(&#39;abcde&#39;)
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=&#39;---&#39;)
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=&#39;asdfg&#39;
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=&#39;asdfghjk&#39;
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=&#39;abc&#39;
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=&#39;abcDEF&#39;
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=&#39;abcDEFasde&#39;
print("a count={0}".format(st1.count(&#39;a&#39;)))
print("{0} start with {1} is {2} ".format(st1,&#39;abc&#39;,st1.startswith(&#39;abc&#39;)))
print("{0} end with {1} is {2} ".format(st1,&#39;de&#39;,st1.startswith(&#39;de&#39;)))
a count=2
abcDEFasde start with abc is True 
abcDEFasde end with de is False 
st1=&#39; abcDEFasde&#39;
print("{0} replace 123 :{1}".format(st1,st1.replace(&#39;abc&#39;,&#39;123&#39;)))
print( "{0} remove {1} left char---{2}".format(st1,&#39; &#39;,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&#39;abc &&#39;
print(type(by))
print("length=",len(by))
<class &#39;bytes&#39;>
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(&#39;输入几个数字逗号隔开:&#39;)
d=ch.split(&#39;,&#39;)
print(d)
sum=0
for num in d:
    sum+=float(num)
print("ths sum=",sum)
输入几个数字逗号隔开:2.2,3.3,5.5,6.8
[&#39;2.2&#39;, &#39;3.3&#39;, &#39;5.5&#39;, &#39;6.8&#39;]
ths sum= 17.8


Related recommendations:

##Practical application of python strings

Detailed explanation of various built-in functions of Python3 strings

Python string separation

Strings and lists in python

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn