java8 stream basic operations

@(table of Contents)

String Manipulation

Indexable string, can be sliced

1. The first letter capitalized, all uppercase, lowercase

name = "love you"
print(name.title())
print(name.upper())
print(name.lower())

When storing data, lower () is useful, because they can not ensure that the user enters the correct case format, it is first converted to lowercase, when the display was converted into the appropriate format.

String concatenation, the digital to STR ()
Python integer division of an integer obtained only

2. Delete blank

message = " 1 2 3 "
print(message.rstrip()) # 去末尾空白
print(message.strip()) # 去两端空白
print(message.lstrip())  # 去开头空白 left right

# 取出全部空白可以用replace()
message.replace(' ','')

3. Repeat

Direct multiply the number of repetitions

>>> a='123'
>>> a*5
'123123123123123'
>>>

4. splicing

Concatenating several different ways

def concat1(): 
    z = x + y 
    return z 

 def concat2(): 
    z = "%s%s" % (x, y) 
    return z 

def concat3(): 
    z = "{}{}".format(x, y) 
    return z 

def concat4(): 
    z = "{0}{1}".format(x, y) 
    return z

5. Find

>>a='abcabcabcxyz'
>>a.count('ab')
3
>>a.find('ca')
2
>>a.find('cx')
8
>>a.find('ca',4,6)
-1
>>a.find('ca',4,9)
5

6. Analyzing Related

Are begins with start: str.startswith('start')
whether to end end: str.endswith('end')
whether the whole letter or digit: str.isalnum()
if the whole letter: str.isalpha()
Are all-digital: str.isdigit()
Are all lowercase: str.islower()
Are all uppercase:str.isupper()

format

In [1]: '{0},{1}'.format('kzc',18)
Out[1]: 'kzc,18'
In [2]: '{},{}'.format('kzc',18)
Out[2]: 'kzc,18'
In [3]: '{1},{0},{1}'.format('kzc',18)
Out[3]: '18,kzc,18'
`
^, <,> Are centered, left aligned, right aligned, the back width;: number of characters after the band filling, only one character is not specified, the default is padded with blanks.
such as

In [15]: '{:>8}'.format('189')
Out[15]: '     189'
In [16]: '{:0>8}'.format('189')
Out[16]: '00000189'
In [17]: '{:a>8}'.format('189')
Out[17]: 'aaaaa189'
print('{:*^60}'.format(' 每个TERMINALNO 数据记录数 ')) 
******************** 每个TERMINALNO 数据记录数 ******************** (中文字符居然只算1个字符,所以有中文字符时候输出不同行还是对不齐)

The form of available digital format

>>'{:0>2}'.format(1)
'01'
>>'{:0>2}'.format(10)
'10'
>>'{:0>2}'.format(100)
'100'

>>'Pi is {:.2f}.'.format(3.1415)
'Pi is 3.14.'

{: B} denotes a binary, b, o, d, x

isdigit () determines whether the number is a pure

>>a='123ac'
>>a.isdigit()
False
>>a='123'
>>a.isdigit()
True

7. String inversion

>>a[::-1]
'321'
>>reversed(a)
<reversed object at 0x0000023FEFC5C6D8>
>>''.join(reversed(a))
'321'

example

>>if a.startswith('-'):
...   a[0]=''
    
Traceback (most recent call last):
  File "<input>", line 2, in <module>
TypeError: 'str' object does not support item assignment
>>if a.startswith('-'):
...   a = a[1:]
>>a
'1--3--5-'
>>if a.endswith('-'):
...  a = a[:-1]
>>a
'1--3--5'
>>a.replace('--','-')
'1-3-5'/

Guess you like

Origin www.cnblogs.com/hqinglau/p/12191578.html