Python Learning Note: string (str) basic content

Source: Internet
Author: User

1. Create a string.

Creating a string is as simple as creating a variable. Use quotation marks to enclose the contents of a string in a single quotation mark, double quotation mark, and three quotation marks.

>>> s = ' abc ' >>> s ' abc ' >>> s = "abc" >>> s ' abc ' >>> s = "" "" abc "" ">>> s ' ABC '

When using single quotes, the content can contain double quotation marks, whereas when using double quotation marks, the content is a single quotation mark at a moment, which means the quotation marks can be used interchangeably.

>>> s = ' abc ' d ' >>> s ' abc "d ' >>> s =" abc ' d ">>> S" abc ' d ">>> s = ' abc ' d ' syntaxe Rror:invalid syntax>>> S = "abc" D "Syntaxerror:invalid syntax

Single quotes automatically escape output when using three quotation marks, and three quotation marks can be wrapped.

>>> s = "" "abc" "" ">>> s ' \na\nb\nc\n '  #直接输出时, escape character not implemented. >>> print (S) ABC

>>> s = "" "ABC ' d   abc" abc "" "   >>> S ' abc\ ' d   abc" ABC ' <span style= "White-space:pre" > </span><span style= "font-family:arial, Helvetica, Sans-serif;" > #直接输出时, single quotes output as escape characters </span>>>> print (S) ABC ' d   ABC ' ABC



2. Use a string.

As with other sequences, it is good to use a slice index for a string. But the string cannot be modified .

>>> S = ' abc ' >>> s[0] ' a ' >>> s[0] = ' B '   #试图修改报错.  Traceback (most recent):  File "<pyshell#27>", line 1, in <module>    s[0] = ' B ' TypeError: ' str ' object does not support item assignment


Slice.

>>> s = ' abcdef ' >>> s ' abcdef ' >>> s[1:3] ' BC ' >>> s[5:1:-2] ' fd ' >>> s[1:3:2] ' B '

Others are similar to lists, tuples, and are not described more in this section.


3. Change the string with ' + '.

The string cannot be modified, but it is also possible to change the contents of the string by using ' + '.

>>> s = ' abcdef ' >>> s ' abcdef ' >>> s = s[1:] + ' python '  #用 1 followed by new content, including 1. >>> S ' Bcdefpython '
<pre name= "code" class= "python" >>>> s = ' abcdef ' >>> s ' abcdef '
>>> S = s[:2] + ' python ' #用 before 2 plus new content, not including 2. >>> S ' Abpython '

>>> s = ' abcdef ' >>> s ' abcdef ' >>> s = s[:] + ' python ' <span style= ' White-space:pre ' ></ span> #没有参数的话直接在最后加上新内容 >>> S ' Abcdefpython '
When it comes to slicing, it contains a coordinate element that does not contain ': ' Before or after it, which seems a bit corny and will not be described later. (Contains ': ' before the coordinate element, does not contain ': ' after the coordinate element)

If there is a step value, it is to keep the corresponding element, and then add the new content.

>>> s = ' abcdef ' >>> s ' abcdef ' >>> s = s[1:3:2] + ' python ' >>> s ' bpython ' >>> s = ' abcdef ' >>> s ' abcdef ' >>> s = s[1:4:2] + ' python ' >>> S ' Bdpython '



4. Delete the string.

Since strings cannot be made, it is not possible to delete only one element, only to clear the contents of a string, and then reassign the value. or remove unwanted elements and then re-form a string.

Empties the string by assigning null values to it. When the string is present, the output is empty.

>>> s = ' abc ' >>> s ' abc ' >>> s = ' >>> s ' >>> s = ' BCD ' >>> s ' BCD '


Delete the string with Del, when the string does not exist, and the output will be error.
>>> s = ' abc ' >>> s ' abc ' >>> del s>>> straceback (most recent call last):  File "< Pyshell#70> ", line 1, in <module>    snameerror:name ' S are not defined


Attempt to delete a single element with Del, error.
>>> s = ' abc ' >>> s ' abc ' >>> s[0] ' a ' >>> del S[0]traceback (most recent call last):  File "<pyshell#74>", line 1, in <module>    del s[0]typeerror: ' str ' object doesn ' t support item deletion


If you want to exclude only one of these elements, then you need to first remove and then re-form a string.

For example, if you want to exclude w in Hello World, you can do so.

>>> s = ' Hello World ' >>> s ' Hello world ' >>> s = s[:6] + s[7:]>>> s ' Hello Orld '



5. String ' + ' ' * ' operation.

Easy.

>>> s = s[:6] + s[7:]>>> s ' Hello orld ' >>> s = ' Hello World ' >>> s ' Hello World ' >>& Gt s + s ' Hello Worldhello world ' >>> s*3 ' Hello Worldhello worldhello world '    


All return the operation of the copy, do not change the value of the original s, if you want to change s, you need to re-assign the equals sign.

>>> s = ' Hello World ' >>> s ' Hello World ' >>> s + s ' Hello Worldhello world ' >>> s    < Span style= "White-space:pre" ></span> #原本S没有改变 ' Hello world ' >>> s = s + S   <span style= " White-space:pre "></span> #原本S改变了 >>> S ' Hello Worldhello World '




6. Member operators.

You can use member operators in a sequence, as well as strings.

In, not in. As the name implies, it is used to determine if an element exists and returns a Boolean value, True or false.

>>> s = ' abc ' >>> s ' abc ' >>> ' a ' in strue>>> ' a ' in sfalse>>> ' a ' not in strue& Gt;>> ' ab ' in strue>>> ' AC ' in Sfalse




The above content in the Python3.5 environment test can be run, because I have just started to contact Python and for self-study, so there is no place to forgive and welcome to point out the error in order to correct!

Python Learning Note: string (str) basic content

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.