Python strings string

Source: Internet
Author: User

Strings string

Grammar:

A = ' Hello world! '

b = "Hello world!"

Common operations:

1, multiplication operation is to repeat the string output 2 times

>>> a= ' abc '
>>> A
' Abcabc '

2, slice operation, the string from the index subscript 2 start slicing to the last.

>>> print ("HelloWorld" [2:])
Lloworld

3, in operation, determines whether the string ABC exists in the string ABCDEFG, returns True if it exists, or false if it does not exist.

>>> ' abc ' in ' ABCDEFG '
True

4.%s indicates formatted output

>>> a= ' Alex '
>>> print ("My name is%s"%a)
My name is Alex

%s stands for string

%d for numbers

%f representing floating-point numbers

5, + (splicing) operator, can be used as stitching characters, string A and string B are stitched into a new string C.

>>> a= ' My name is '
>>> b= ' Alex '
>>> c=a+b
>>> Print C
My name is Alex

6. Join () method is the stitching method

>>> a= "12345678"
>>> b= "_". Join (a)
>>> b
' 1_2_3_4_5_6_7_8 '

>>> c= "_". Join ([A, b])
>>> C
' 12345678_ABCDEFG '

"Stitching character". Join () joins the list into a string, and the elements in the list are stitched together into a string by a custom stitching character.

Common methods:

st = ' Hello Kitty '

1. The counter () method gets the number of occurrences of the character E in the string.

>>> st.count (' e ')
1

2. The capitalize () method converts the first character of a string to uppercase characters.

>>>st.capitalize ()

' Hello Kitty '

3, the Center () method can center the string output, both sides with 50 underscores to complement, can be used to describe the character of the center output.

>>> St.center (50, ' _ ')
' ___________________hello kitty____________________ '

4. The EndsWith () method is used to determine whether to end with a character, or False if True is returned.

>>> st.endswith (' y ')
True

5. The Startswitch () method is used to determine whether to start with a character, or return true if it is, otherwise false.

>>> st.startswith (' e ')
False
>>> st.startswith (' h ')
True

6, Expandtabs () method is to control the number of tabs in the string, the default tab is four spaces, this method can be used to define the tab space, which is defined as 20.

>>> bt= ' He\tllo Kitty '

>>> Bt.expandtabs (tabsize=20)

He llo Kitty

7. The find () method is used to find the position where the character element first appears in the string and returns the index position. You can only query where the first occurrence occurs. did not find no error.

>>> st.find ("E")
1

8. The format () method is used to format the output, for example, the parameter definition name in format, where name will pass its own value to the {name} in the St variable, and the final output will look like this.

>>> st= ' Hello Kitty {name} '
>>> St.format (name= ' Alex ')
' Hello Kitty Alex '

>>> st= ' Hello Kitty {name} ' {age} '
>>> St.format (name= ' Alex ', age= ' 27 ')
' Hello Kitty Alex is 27 '

9, the Formap_map () method is also used to format the output, the function and Formap, just like the Formap_map parameter is in the form of a dictionary assignment.

>>> st= ' Hello Kitty {name} ' {age} '

>>> St.format_map ({"Name": ' Alex ', "Age": ' 27 '})

' Hello Kitty Alex is 27 '

10. The index () method is similar to the Find method and is used to find the first occurrence of a character in a string, and returns the position subscript, but if index is not found, an error is returned.

>>> st.index (' e ')
1

>>> St.index (' 4 ')
Traceback (most recent):
File "<stdin>", line 1, in <module>
Valueerror:substring not found

11. The Isalnume () method is used to detect whether a string is alphanumeric and returns false if a special character is included, otherwise true.

>>> st= ' abc345 '

>>> St.isalnum ()
True

12. The IsDigit () method is used to determine whether a string is a number. The return value is true and false.

>>> "123". IsDigit ()
True
>>> ' abc '. ISDIGIT ()
False

13. The IsNumeric () method is used to determine whether a string is a number, function, and usage with the IsDigit () method. The return value is true and false.

14. The Isidentifier () method is used to determine whether a variable is named illegally. The return value is true and false.

>>> ' abc '. Isidentifier ()

True

>>> ' 123abc '. Isidentifier ()

False

15. The Islower () method is used to determine whether the string is all lowercase and returns false if there are uppercase characters.

>>> st= ' abc '
>>> St.islower ()
True
>>> st= ' ABC '
>>> St.islower ()
False

16, Isupper () method function with Islower idea, used to determine whether the string is all uppercase, if there is lowercase, return false.

>>> st= ' abc '
>>> St.isupper ()
False
>>> st= ' ABC '
>>> St.isupper ()
True

17, the Isspace () method is used to determine if all are spaces, and the return value is true and false.

>>> st= ' ab C '
>>> St.isspace ()
False
>>> st= ""
>>> St.isspace ()
True

18. The Istitle () method is used to determine whether the first character of all strings in the header is uppercase, and if not, returns false.

>>> st= ' My Title '
>>> St.istitle ()
True
>>> st= ' My title '
>>> St.istitle ()
False

19, Lower () method, used to convert all strings to lowercase characters.

>>> St
' My title '
>>> St.lower ()
' My title '

20, Upper () method, used to convert all strings into uppercase characters.

>>> St
' My title '
>>> St.upper ()
' MY TITLE '

21, Swapcase () method, used to invert all characters of a string, uppercase to lowercase, lowercase to uppercase.

>>> st= ' MY title '
>>> St.swapcase ()
' My TITLE '

22, Strip () method, used to remove all whitespace and line breaks at the beginning and end of a string. Often used to get the screen input.

>>> St
' My title \ n '
>>> St.strip ()
' My title '

23.

Python strings 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.