Talking about the Python string

Source: Internet
Author: User
A python string is a sequence type in which an element is a character. Because sequence types are a data structure in which elements are placed sequentially, you can either get a character by index or specify an index range to get a set 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 crossed out from 0 to Lne (str)-1, otherwise an error occurs.

>>> len (CH) 5>>> Ch[5]traceback (most recent call last):  File "<pyshell#4>", line 1, in <mo Dule>    ch[5]indexerror:string index out of range

Reverse print string

def resstr (CH):    mid=len (CH) for    C in range (mid):        print (ch[mid-1-c],end= ") >>> resstr (' ABCDE ') Edcba

SB's preparation, reverse string, string cannot be modified. Error message: ' str ' object does not support item assignment

The Shard of a string is the separation of a part of a string from a given string, which can be indexed in the following form I,j,k
I is the starting position, J is the index end position, but does not include the string in the J position, the index number increments at each increment of K

>>> s= "Hello World" >>> print (S[0:len (s): 2]) hlowrd>>> print (S[1:len (s): 3],end= '---') Eood---

Indexed index, which is from 0 to Len (str)-1, can also use a negative index, 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 the string shard, the starting position of the index I, the index end position J, and the step K can be omitted, omit I, starting from 0 or 1, omit J when the last string ends, omit the K time step is 1.

st= ' ASDFGHJK ' Print (St[:0:-1]) print (St[2::2]) print (st[0:5:]) KJHGFDSDGJASDFG

string-related operations, which can be used for connection operations, logical operations, and string handling 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= Abcdefabc*5=abcabcabcabcabc

Method of String
The string is immutable, and any string that changes the string will return a new string, and the Python string can be considered 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 () =abcdefabcdef.lower () = Abcdefabcdef.swapcase () =abcdefst1= ' 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=2abcdefasde start with ABC was 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

The method of string is too many, not say, no meaning.

Byte object
In Python, a sequence of non-immutable Unicode characters is called a string, unlike Byte and string. A sequence of strings that cannot be changed to encode between 0--255 is called a byte object.

By=b ' ABC & ' Print (Type (by)) print ("Length=", len) <class ' bytes ' >length= 5

You can define a byte object with a ' B ' in front of the string, each string can be an ASCII character, and you can use the Len () function to calculate the length of a byte object.

Ch=input (' Enter several numbers comma separated: ') d=ch.split (', ') print (d) sum=0for num in D:    sum+=float (num) print ("Ths sum=", sum) Enter several numbers comma separated: 2.2,3.3,5.5,6.8[' 2.2 ', ' 3.3 ', ' 5.5 ', ' 6.8 ']ths sum= 17.8


Related recommendations:

Python string actual application

Python3 string various built-in functions detailed

Python string delimited

String and list of Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.