[Python Basics] str string, pythonstr string

Source: Internet
Author: User

[Python Basics] str string, pythonstr string

The str string is an unchangeable object.

1. Create a string
S1 = str () # create an empty string s2 = str ("hello") # create a string "hello"

 

2. Common functions and operations for processing strings (1). Functions
Len () Returns the number of characters in a string.
Max () Returns the largest character in a string.
Min () Returns the smallest character in a string.

>>>s = "Welcome">>>len(s)7>>>max(s)'o'>>>min(s)'W'

 

The full text of string s is common and will not be described below

(2) The range of the subscript operator [] s [index] index is [0, len (s)-1]. Note: python allows negative numbers to be the most subscript.
>>>print(s[6], s[4])e o>>>print(s[-1], s[-3])e o

 


(3 ). the Truncation operator [start: end] [start: end] intercepts and returns a string whose subscript starts from start to end-1 in string s. If start> end, an empty string is returned.
>>> S [] 'elc' >>> s [1:-1] # You can also use the negative 'elcom' >>> s [3: -8] # truncate and return an empty string''

 

(4). Join operator + and copy operator * +: connect two strings. -: Copy strings.
>>> S1 = "hello" >>> s2 = "world" >>> s1 + ''+ s2 'Hello world' >>> 3 * s1 # and s1 * 3 same as 'hellohellohellohel'

 


(5). Determine whether a string is in another string (in and not in). For example, s is the string "Welcome"
>>> 'Come in s # if it is true, tureTrue is returned >>> 'cat' in s # If it is false, falseFalse is returned >>> 'cat' not in sTrue

 


(6). Comparison string (= ,! =,>, <, >=, <=) Python compares characters in strings. Start from the first character. If the first character is the same, the second character is compared, and so on. If the expression is True, True is returned; otherwise, False is returned.
>>> S1 = 'integer' >>> s2 = 'int' >>> s1 = s2False >>> the ARCII code value of s1 <s2 # 'E' is greater than 0, therefore, falseFalse >>> s1 >=s2true is returned.

 

(7). substring generation string (with for loop) yi generation string s:
>>> for ch in s:    print(ch)    Welcome>>> 
3. Other strings (1). Test strings
Isalnum (): bool Returns true if the string is a letter or number and has at least one character.
Isalpha (): bool Returns true if the string is a letter and has at least one character.
Isdigit (): bool Returns true if the string contains only numeric characters.
Isdentifier (): bool Returns true if the string is a python identifier.
Islower (): bool Returns true if all characters in the string are lowercase and contain at least one character.
Isupper (): bool Returns true if all characters in the string are uppercase and contain at least one character.
Isspace (): bool Returns true if all characters in the string are spaces and contain at least one character.


(2). SEARCH strings
Startswitch (s1: str): bool Returns true if the substring is s1.
Endswitch (s1: str): bool Returns true if the string ends with a substring of s1.
Find (s1): int Returns the lowest subscript of s1 in the string.-1 is returned if no subscript exists.
Rfind (s1): int Returns the highest subscript of s1 in the string.-1 is returned if no subscript exists.
Count (sub string): int Returns the number of times that the substring appears in the string.
(3). Convert the string
Capitalize (): str Returns the copied string with the first character in uppercase.
Lower (): str Returns the copied string and converts all the letters to lowercase letters.
Upper (): str Returns the copied string and converts all letters to uppercase letters.
Title (): str Returns the copied string and capital the first letter of each word.
Swapcase (): str Returns the copied string, converts uppercase letters to lowercase letters, and converts lowercase letters to uppercase letters.
Replace (old, new): str Returns the new string, and replaces all old strings with new.
(4). Delete spaces in the string
Lstrip (): str Returns the substring that removes the front-end blank string.
Rstrip (): str Returns the substring that removes the backend blank string.
Strip (): str Returns the substring that removes the empty strings at both ends.
(5). format the string
Center (width): str Returns a string copy centered on a given width field.
Ljust (width): str Returns the left-aligned string text in the given width field.
Must ust (width): str Returns the right-aligned string text in the given width field.
Format (items): str  


 

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.