python string some common tips


String manipulation is often used, even to the interview, the interviewer will examine candidates will ask a few basic questions string handling, I tried to get myself used to initiate the process to share out the string, welcome shot brick

Cutting strings

Split string can be directly implemented split function, it returns a list of

print(str1.split(" "))
#output
['hello', 'python']

Here mention a function strip (), his role is to remove the default string inclusive spaces

str2 = "  hello  "
print(str2.strip())
#output
hello

The combined string

list1 = ["hello","python"]
print(" ".join(list1))
#output
hello python

The combined use of a string of python join () function, the elements of a sequence to the specified character (here, a space) is connected generates a new string

String fragment

python string fragment which is very simple, Ado, directly look at the code

str1 = "hello python"
print(str1[2:8])
#output  llo py
print(str1[:2])
#output  he
print(str1[:-2])
#output  hello pyth
print(str1[-5:-2])
#output  yth

From the results should be able to see it, the default is counted from the first character, you can also set up their own start string, the negative sign indicates the start of the count from the end of the string
, but should be noted that this fragment is not the true sense of cutting length does not change, and the split function of the original string is not the same

Inversion interval and the string

Similar flip and above the usage string, but it should be more a colon

print(str1[::-1])
print(str1[::-2])
print(str1[::1])
print(str1[::2])

#output
nohtyp olleh
nhy le
hello python
hlopto

The negative sign here says is reversed, and the number indicates the number of character spacing

Analyzing string palindrome

Palindrome judge can use a string of flip tricks

if str1 == str1[::-1]:
    print("是回文字符串")
else:
    print(str1 +"不是回文字符串")
#output
hello python不是回文字符串

The case of a string of words

print(str1.title()) #单词首字母大写
print(str1.upper()) #单词所有字母大写
print(str1.upper().lower()) #单词所有字母小写
print(str1.capitalize()) #字符串字母大写

#output
Hello Python
HELLO PYTHON
hello python
Hello python

Determining whether the string of elements contained in the same

Note that we are to judge the string contains the same elements are not equal, direct look at an example, this would be a little more aware

from collections import Counter
str1 = "hello"
str2 = "hlloe"
str3 = "lolhe"
cn_str1,cn_str2,cn_str3 = Counter(str1),Counter(str2),Counter(str3)
print(cn_str1,cn_str2,cn_str3)
if(cn_str1==cn_str2 and cn_str2==cn_str3):
    print("所含元素相同")

#output
Counter({'l': 2, 'h': 1, 'e': 1, 'o': 1}) Counter({'l': 2, 'h': 1, 'o': 1, 'e': 1}) Counter({'l': 2, 'o': 1, 'h': 1, 'e': 1})
所含元素相同

We judge the same elements are ignored by order of the elements, is simply whether the number of elements and each element of each string of statistics about the occurrence of the same, so that's what we will use here python library for statistical
us also the way to look at the number of times it appears most of the elements, for example to str1

print(Counter(str1).most_common(2))
#output:
[('l', 2), ('h', 1)]

Here we print is the largest number of occurrences of the first two elements

The string of numbers into a digital list

str1 = "123456"
list1 = list(map(int,str1))
print(list1)
#output
[1, 2, 3, 4, 5, 6]

Without such a deal, directly to the list, the result would be something like

print(list(str1))
#output
['1', '2', '3', '4', '5', '6']

Such is not saved to the list of numbers, but strings, here I will briefly explain the application map function
map () will do the mapping function in accordance with the specified sequence provided. The first parameter argument function to call the function function of each element in the sequence, each time the function returns a new list contains the function return value, the general usage of a particular

map(function, iterable,iterable...)

Here we use the map is equivalent to each element of the string are converted to int data type is stored in the list

Published 21 original articles · won praise 28 · views 3708

Guess you like

Origin blog.csdn.net/LPJCSY/article/details/104835125
Recommended