01-common operations on strings in python, 01-python strings

Source: Internet
Author: User

01-common operations on strings in python, 01-python strings

(1) find

Check whether str is included in myStr. If yes, the initial index value is returned. Otherwise,-1 is returned.

In [1]: myStr = "hello world tairan and tairanCity."In [2]: str = "tairan"In [3]: myStr.find(str)Out[3]: 12

In [7]: str = "helo"

In [8]: myStr.find(str)
Out[8]: -1

(2) index

Search for indexes.

In [9]: myStr = "hello world tairan and tairanCity" In [10]: myStr. find ("world") Out [10]: 6In [11]: myStr. find ("tairan") Out [11]: 12In [12]: myStr. rfind ("tairan") Out [12]: 23 # rfind indicates a query from right to left.

(3) count

Returns the number of times that str appears in myStr between start and end.

In [13]: myStrOut[13]: 'hello world tairan and tairanCity'In [14]: myStr.count("hello")Out[14]: 1In [15]: myStr.count("tairan")Out[15]: 2

(4) replace

Replace 'str' in myStr with new_str. If 'Count' is specified, it cannot exceed 'Count.

In [16]: myStrOut [16]: 'Hello world tairan and tairancity' In [17]: myStr. replace ("hello", "nihao") Out [17]: 'nihao world tairan and tairancity' In [18]: myStr. replace ("tairan", "TaiRan") Out [18]: 'Hello world TaiRan and tairancity' In [20]: myStr. replace ("tairan", "Tairan", 1) # If count is specified, replace the value cannot exceed count times Out [20]: 'Hello world Tairan and tairanCity'

(5) split

Cut, which uses str as the separator. If maxsplit has a specified value, only the maxcompute strings are separated.

In [21]: myStrOut [21]: 'Hello world tairan and tairancity' In [22]: str = "" In [23]: myStr. split (str) Out [23]: ['hello', 'World', 'tairance', 'and', 'tairancity']

In [24]: myStr. split (str, 2) # specify count as 2
Out [24]: ['hello', 'World', 'tairan and tairancity']

(6) capitalize

The first character of the string is capitalized.

In [25]: myStrOut[25]: 'hello world tairan and tairanCity'In [26]: myStr.capitalize()Out[26]: 'Hello world tairan and tairancity'

(7) title

The first letter of each word in the string is capitalized.

In [27]: myStrOut[27]: 'hello world tairan and tairanCity'In [28]: myStr.title()Out[28]: 'Hello World Tairan And Tairancity'

(8) startswith

Checks whether the string starts with obj. If yes, True is returned. Otherwise, False is returned.

In [29]: myStrOut[29]: 'hello world tairan and tairanCity'In [30]: myStr.startswith("hello")Out[30]: TrueIn [32]: myStr.startswith("helo")Out[32]: False

(9) enddswith

Check whether the string has ended with obj. If yes, True is returned. Otherwise, False is returned.

In [33]: myStrOut[33]: 'hello world tairan and tairanCity'In [34]: myStr.endswith("ty")Out[34]: TrueIn [35]: myStr.endswith("txt")Out[35]: False

(10) lower

Converts all uppercase characters in mystr1.

In [36]: myStr1 = "HELLO WORLD TAIRAN AND TAIRANCITY"In [37]: myStr1.lower()Out[37]: 'hello world tairan and tairancity'

(11) upeer

Converts all lowercase characters in myStr to uppercase.

In [38]: myStrOut[38]: 'hello world tairan and tairanCity'In [39]: myStr.upper()Out[39]: 'HELLO WORLD TAIRAN AND TAIRANCITY'

(12) ljust

Returns a new string with the left alignment of the original string and filled with spaces to the width.

In [40]: str = "hello"In [41]: str.ljust(10)Out[41]: 'hello     '

(13) Just UST

Returns the right of an original string.

In [42]: strOut[42]: 'hello'In [43]: str.rjust(10)Out[43]: '     hello'

(14) center

Returns a new string with the original character centered and filled with spaces to the width.

In [45]: str.center(10)Out[45]: '  hello   '

(15) lstrip

Delete the white space characters on the left of myStr.

In [47]: myStr = "       hello"In [48]: myStr.lstrip()Out[48]: 'hello'

(16) rstrip

Delete the white space characters on the Right of myStr.

In [49]: myStr = "hello         "In [50]: myStr.rstrip()Out[50]: 'hello'

(17) strip

Delete white spaces at both ends of the myStr string.

In [51]: myStr = "    hello       "In [52]: myStr.strip()Out[52]: 'hello'

(18) partition

Split myStr into three parts: str before, str, and str.

In [53]: myStr = "hello world tairan and tairanCity. "In [54]: str =" tairan "In [55]: myStr. partition (str) Out [55]: ('Hello world', 'tairanc', 'and tairanCity. ') # The returned result is a tuples with spaces In [56]: myStr. rpartition (str) Out [56]: ('Hello world tairan and ', 'tairan', 'city. ') # rpartition indicates searching from right to left

(19) splitlines

Returns a list containing rows as elements.

In [57]: myStr = "hello\nworld"In [58]: print(myStr)helloworldIn [59]: myStr.splitlines()Out[59]: ['hello', 'world']

(20) isalpha

If all characters in myStr are letters, True is returned; otherwise, False is returned.

In [61]: myStrOut[61]: 'danjk'In [62]: myStr.isalpha()Out[62]: TrueIn [63]: myStr1 = "1244"In [64]: myStr1.isalpha()Out[64]: False

(21) isdigit

If myStr only contains numbers, True is returned; otherwise, False is returned.

In [66]: myStr = "213131"In [67]: myStr.isdigit()Out[67]: TrueIn [68]: myStr = "dmakd"In [69]: myStr.isdigit()Out[69]: False

(22) isalnum

Returns True if the string is a character or number; otherwise, returns False.

In [70]: myStr = "21da2"In [71]: myStr.isalnum()Out[71]: TrueIn [72]: myStr = "2112123"In [73]: myStr.isalnum()Out[73]: True

(23) isspace

If the string contains at least one letter and all characters are spaces, True is returned. Otherwise, False is returned.

In [74]: myStr = "       "In [75]: myStr.isspace()Out[75]: TrueIn [76]: myStr = "   dad       "In [77]: myStr.isspace()Out[77]: False

(23) join

Insert str after each character in myStr to construct a new string.

In [78]: str = " "In [79]: myStr = ["my","name","is","xiaoyafei"]In [80]: str.join(myStr)Out[80]: 'my name is xiaoyafei'
In [81]: str = "_"In [82]: str.join(myStr)Out[82]: 'my_name_is_xiaoyafei'

 

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.