In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "python how to deal with strings", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "python how to deal with strings" this article.
String processing skills
1. Clean up user input
It is a common program requirement to clean up the entered values. For example, to do case conversion, to verify the injection of input characters, you can usually write regularities and use Regex to do special tasks. But for complex situations, you can use some techniques, such as the following:
User_input = "This\ nstring has\ tsome whitespaces...\ r\ n" character_map = {ord ('\ n'):', ord ('\ t'):', ord ('\ r'): None}
In this example, you can see that the space characters "\ n" and "\ t" are replaced with spaces, while "\ r" is deleted.
This is a simple example, and we can also use the unicodedata package and the combinin () function to generate a large mapping table to generate a mapping to replace the string.
two。 Prompt the user for input
A command line tool or script requires a user name and password to operate. A useful technique to use this feature is to use the getpass module:
Import getpass user = getpass.getuser () password = getpass.getpass ()
These three lines of code allow us to interact gracefully to remind the user to enter the password and capture the current system user and the entered password, and automatically block the display when entering the password to prevent it from being stolen.
3. Find string frequency
If you need to find words similar to some input strings, you can use difflib to do this:
Import difflib difflib.get_close_matches ('appel', [' ape', 'apple',' peach', 'puppy'], nasty 2)
# return ['apple',' ape']
Difflib.get_close_matches looks for the string with the most similar match. In this case, the first parameter matches the second parameter. Provides the optional parameter n, which specifies the maximum number of matches to return, and the parameter cutoff (default = 0.6) is set to thr to determine the score of the matching string.
4. Multiline string
Backslashes can be used in Python:
In [20]: multistr = "select * from test\...: where id
< 5" In [21]: multistr Out[21]: ' select * from test where id < 5' 还可以使用三引号: In [23]: multistr ="""select * from test ...: where id < 5""" In [24]: multistr Out[24]: 'select * test where id < 5' 上面方法共有的问题是缺少合适的缩进,如果我们尝试缩进会在字符串中插入空格。所以最后的解决方案是将字符串分为多行并且将整个字符串包含在括号中: In [25]: multistr = ("select * from multi_row " ...: "where row_id < 5 " ...: "order by age") In [26]: multistr Out[26]: 'select * from multi_row where row_id < 5 order by age' 5. 处理IP地址 日常常用的一个是验证和匹配IP地址,这个功能有个专门的模块ipaddress可以来处理。比如我们要用IP网段(CIDR用IP和掩码位)生成一个IP地址列表: import ipaddress net = ipaddress.ip_network('192.168.1.0/27') 结果: #192.168.1.0 #192.168.1.1 #192.168.1.2 #192.168.1.3 #... 另一个不错的功能IP地址是否在IP段的验证: ip = ipaddress.ip_address("192.168.1.2") ip in net # True ip = ipaddress.ip_address("192.168.1. 253") ip in net # False ip地址转字符串、整数值的互转: >> > str (ipaddress.IPv4Address ('192.168.0.1')) '192.168.0.1' > int (ipaddress.IPv4Address ('192.168.0.1')) 3232235521 > str (ipaddress.IPv6Address (':: 1'))':: 1' > int (ipaddress.IPv6Address (':: 1')) 1
Note that ipaddress also supports many other features, such as ipv4 and ipv6.
These are all the contents of the article "how python handles strings". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.