In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to understand Python strings, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, people who have this need can learn, I hope you can gain something.
1. Basic characteristics of strings
The essence of a string is that it is a sequence of characters, and Python strings are immutable, and we cannot make any changes to the original string.
But you can copy a portion of a string to a newly created string to "look modified."
Python does not support single-character types, which are also used as strings.
2. Quotation marks create strings
We can create strings with single quotes or double quotes. For example: a='abc'; b='jea'
The advantage of using both quotes is that you can create strings that contain quotes themselves instead of using escape characters, such as
>>> a = "I'm a teacher! ">>> print(a)I'm a teacher!>>> b = 'my_name is "TOM"'>>> print(b)my_name is "TOM"3. Empty strings and len() functions--Python allows empty strings to exist that contain no characters and have a length of 0>>> c = '>> len(c)0len() to calculate how many characters a string contains. For example: >>> d = 'IT Bond'>>> len(d)44. Escape character
We can use "\+ special characters" to achieve certain effects that are difficult to express in characters.
For example: new line etc. Common escape characters have these
>>> a = 'I\nlove\nU'>>> a'I\nlove\nU'>>> print(a)IloveU>>> print ('aaabb\cccddd') aaabbcccddd5. Print without line breaks
When we called print earlier, it automatically printed a newline character. Sometimes, we don't want a line break, we don't want to add line breaks automatically.
We can do this ourselves by passing the argument end = "arbitrary string". Add anything at the end of the implementation:
print("jea",end ='')print("jea",end ='##')print("jea") Run result: jea jea##jea6.replace() Implement string substitution
String cannot be changed. However, we do sometimes need to replace certain characters. This can only be done by creating a new string
>>> a = 'abcdefghijklmnopqrstuvwxyz'>>> a'abcdefghijklmnopqrstuvwxyz'>>> a = a.replace ('c ',' high')'ab highdefghijklmnopqrstuvwxyz'7.split() split and join() merge
split() separates a string into multiple substrings (stored in a list) based on the specified delimiter.
If no delimiter is specified, white space characters (newline/space/tab) are used by default. The sample code is as follows:
>>> a = "to be or not to be">>> a.split()['to', 'be', 'or', 'not', 'to', 'be']>>> a.split('be')['to ', ' or not to ', '']
Join() does the opposite of split() and is used to join a series of substrings.
The sample code is as follows:
>>> a = ['sxt','sxt100','sxt200']>>> '*'.join(a)'sxt*sxt100*sxt200'
8. other methods
1. isalnum() is a letter or number
2. isalpha() checks whether a string consists of letters only (including Chinese characters).
3. isdigit() checks whether a string consists of numbers only.
4. isspace() checks whether it is a blank character
5. Is isup () a capital letter?
6. islower() is a lowercase letter
>>> "sxt100".isalnum()True>>> "sxt fatness".isalpha()True>>> "234.3".isdigit()False>>> "23423".isdigit()True>>> "aB".isupper()False>>> "A".isupper()True>> "\t\n".isspace()True
Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.
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.