Python learning path-string 1

Source: Internet
Author: User

Python learning path-string 1
1 string

The string type is the most common type in Python. We can create a quotation mark ('and "have the same effect) by including characters. A string is a scalar and an unchangeable type. A string is composed of independent characters, which can be accessed through the slice operation sequence.

1.1 create and assign values to strings

>>> aStr = 'hello world'>>> another = "hello csdn">>> aStr'hello world'>>> another'hello csdn'>>> s = str(range(5))>>> s'[0, 1, 2, 3, 4]'>>> 

1.2 access string value

Python does not have this type of character, but is represented by a string with a length of 1. You can use the slice operation to obtain characters or substrings.

>>> aStr'hello world'>>> aStr[2]'l'>>> aStr[2:]'llo world'

1.3 change the string

You can change the string by assigning a value to the variable.

>>> aStr'hello world'>>> aStr = aStr[:6] + ' python'>>> aStr'hello  python'>>> 

The string type is immutable. To change a string, you must create a new String object.

1.4 Delete characters and strings

The string is unchangeable, so you cannot delete a character in a string. All you can do is Delete the entire string. If you want to delete a character, you can combine the unneeded strings into a new string. For example, if you want to delete 'O', you can use the del statement to delete the string.

>>> aStr = 'hello man'>>> aStr = aStr[:4] + aStr[5:]>>> aStr'hell man'>>> del aStr

2 string and operator 2.1 Standard Type Operator
>>> str1 = 'abc'>>> str2 = 'def'>>> str3 = 'kmn'>>> str1 < str2True>>> str1 != str2True>>> str1 < str3 and str2 == 'def'True>>> 
Strings are compared based on the ASCII value.
2.2 sequence operator slice forward index:
>>> str = 'abcdefg'>>> str[0:3]'abc'>>> str[2:5]'cde'>>> 
Reverse index:

>>> str[-6:-1]'bcdef'>>> str[-8:-1]'abcdef'>>> 

Default index: If the index is started or the description index is not specified, the first and last index values of the string are the default values. If the two are not specified, the entire string is returned.
>>> str[:4]'abcd'>>> str[4:]'efg'>>> str[:]'abcdefg'>>> 

2.3 The in (not in) member operator is used to determine whether a character or a substring appears in another string. If yes, true is returned. Otherwise, False is returned.
>>> 'bc' in 'abcd'True>>> 'n' in 'abcd'False>>> 'dd' not in 'abcd'True>>> 

The 2.4 connector (+) can connect two strings to form a new string.
>>> 'this is str1' + ' this is str2''this is str1 this is str2'>>> s = 'xxx' + ' ' + 'is a good' + ' ' + 'student'>>> s'xxx is a good student'

This kind of connection is called the runtime connection, and another kind of connection is the compile-time connection (not commonly used, depending on personal habits)
>>> text = 'hello' 'world!'>>> text'helloworld!'>>> 





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.