Python Base 4-string

Source: Internet
Author: User

A python string is a string of characters made up of numbers, letters, and underscores, and we can use quotation marks to create strings. Such as:
str = ' Helloworld '
There is no char type in Python, and a single character is also used as a string;

The list of strings in Python has 2 order of values:
A. From left to right, the default index starts at 0 and the index length is the longest of the string length-1
B. From right to left, default index starts from-1

Python accesses substrings, you can use square brackets to intercept strings, such as:
>>> str = ' Helloworld '
>>> Str[0]
H
#左边的开始是包含了下边界. The following result contains the value E of str[1], and the maximum range taken is not including the upper boundary, or str[5]
The Value O
>>> Str[1:5]
' Ello '
>>>

Print str               # output entire string
Print Str[0]           # The first character in the output string
Print Str[1:5]       # The string between the second and the fifth in the output string
Print Str[1:7:2]    # Every step of the output string A string between the second and seventh
Print Str[2:]        # output A string starting from the third character
Print str*2         # output String two times
Print str+ ' test '   # output concatenated string

------------------------------
Characters commonly used in Python:
\ #用到特殊符号时需使用转义字符
+ #字符串连接
* #连续输出多个同一字符 such as str*2
[:] #输出字符串的一部分
In #若字符在字符串中则返回True
Not in #若字符不在字符串中则返回True
------------------------------
Commonly used string built-in functions:
>>> str.count (' l ') #统计str字符串中字符 ' l ' occurrences
3
>>> str.find (' o ') #计算str字符串中字符 ' o ' where the index appears
4
>>> str.index (' o ') #计算str字符串中字符 ' o ' where the index appears
4
>>> str.replace (' l ', ' 2 ') #把str字符串中的字符 ' l ' replaced with ' 2 '
' He22owor2d '
------------------------------

Python Base 4-string

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.