Python and the characteristics of the character string common methods

Characteristics of the string

Index: (indexed starting from 0)

= S 'Hello'
Print (S [0])
Print (S [. 4])
Print (S [-1]) # come last character

H
O
O
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
taken s [start: stop: step] beginning at start to the end stop, the step of step

print (s [0:. 3])
print (s [0:. 4: 2])
print (s [:]) # displays all characters
print (s [: 3]) prior to # display three characters
print (s [ 1:]) # except the first character of all the other characters in
print (s [:: - flip 1]) # string

HEL
HL
Hello
HEL
ello
olleh
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
repeated

print(s * 10)

hellohellohellohellohellohellohellohellohellohello
1
2
3
连接

print('hello ' + 'python')

hellopython
. 1
2
. 3
members operator

print('he' in s)
print('aa' in s)
print('he' not in s)

True
False
False
. 1
2
. 3
. 4
. 5
. 6
. 7
for loop iterates

I in S for:
Print (I)

H
E
L
L
O
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
------------- ------------ Exercise 1

Determine whether an integer is a palindrome.

# Nmb = input ( 'Enter a number:')
# NMBS NMB = [:: -1]
# IF (NMB == NMBS):
# Print ( 'This number is a palindrome')
# the else:
# Print ( 'this number is not a palindrome number')
. 1
2
. 3
. 4
. 5
. 6
without the string characteristics

# Nmb = input ( 'Enter number:')
# C = len (NMB)
# B = int (C / 2)
# A = 0
# for J in Range (B):
! # IF (NMB [A] = NMB [C -. 1 - a]):
# Print ( "not a palindrome number ')
# BREAK
# the else:
# Print (' a palindrome ')
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
common method string

Determine whether the title, uppercase, lowercase, and capitalization, the title of the conversion, the first letter of a string of uppercase letters all lowercase rest
str.istitle ()
str.isupper ()
str.islower ()
str.title () # will the first letter of all words in a string of capital, remaining letters all lowercase; the word in any punctuation distinction
str.captialize () # the first letter of a string of uppercase, all lowercase letters remaining
str.upper ()
str.lower ( )

>>> 'Hello'.istitle()
True
>>> 'hello'.istitle()
False
>>> 'hello'.isupper()
False
>>> 'Hello'.isupper()
False
>>> 'HELLO'.isupper()
True
>>> 'hello'.islower()
True
>>> 'HELLO'.islower()
False
>>> 'HELLO'.lower()
'hello'
>>> 'hello'.upper()
'HELLO'
>>> 'hello'.title()
'Hello'a prefix and suffix match20 is. 1918 is. 1716151413 is12 is. 1110. 9. 8. 7. 6. 5. 4. 32
. 1



















filename = 'hello.logrrrr'
if filename.endswith('.log'):
print(filename)
else:
print('error.file')

error.file

= URL 'https://172.25.254.250/index.html'
IF url.startswith ( "HTTP: //"):
Print ( 'crawled pages')
the else:
Print ( 'can not crawl')

can not crawl
1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
removal spaces left and right sides, the space of generalized space comprising: \ t \ n

>>> s = ' hello '
>>> s
' hello '
>>> s.lstrip()
'hello '
>>> s.rstrip()
' hello'
>>> s.strip()
'hello'
>>> s = '\t\thello \n\n'
>>> s
'\t\thello \n\n'
>>> s.strip()
'hello'
>>> s.rstrip()
'\t\thello'
>>> s.lstrip()
'hello \n\n'
>>> s = 'helloh'
>>> s.strip('h')
'ello'
>>> s.strip('he')
'llo'
>>> s.lstrip('he')
'gejayan ' 141312111098765432
1













15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
string Analyzing

str.isdigit () # Digital
str.isalpha () # full alphabet
str.isalnum () # only numbers and letters

-------------Exercise--------------

To determine the legality of variable names defined:
1. Variable names can consist of alphanumeric underscores
2. The variable name and can only letters or underscore

sname = input('请输入变量名:')
if not (sname[0].isalpha()) or sname[0] == '_':
print('illegal')
else:
a =0
for i in sname:
if sname[a] =='_' or sname[a].isalnum():
a += 1
else:
print('illegal')
break
else:
print('合法')

OR -------------------- ----------------
# the while True:
# S = INPUT ( 'variable names: ')
# S IF ==' Exit ':
# Print (' Exit ')
# BREAK
# IF S [0] .isalpha () or S [0] ==' _ ':
# for I in S [. 1:] :
# IF not (i.isalnum () or i == '_'):
# Print ( '% S variable name is not valid'% (S))
# BREAK
#
# the else:
# Print ( '% S variable name legally '% (S))
#
# the else:
# Print ('% S variable name is not valid '%(s))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 is
24
25
26 is
27
28
29
30
31 is
aligned with the string

print ( 'jjj'.center (30))
print ( "jjj'.center (30, +))
print (" jjj'.ljust (30,' # '))
print ( "jjj'.rjust (30 , '$'))

jjj
************* ************** jjj
jjj #################### #######
$$$$$$$$$$$$$$$$$$$$$$$$$$$ JJJ
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
search and replace string

= S 'Hello World Hello'
. 1
#find found substring, and returns the minimum index

Print (s.find ( 'hello'))
Print (s.find ( 'World'))
Print (s.rfind ( 'hello'))
. 1
2
. 3
# in the replacement string is hello westos

Print (s.replace does ( 'Hello', 'westos'))
. 1
statistical character string and the string length

print('hello'.count('l')) 2
print('hello'.count('ll')) 1

Print (len ( 'westosssss')) 10
. 1
2
. 3
. 4
strings and connecting divided

# Divided by the specified connector

s = '172.25.254.250'
s1 = s.split('.')
print(s1)
print(s1[::-1])

= DATE '2019-05-24'
date1 = date.split ( '-')
Print (date1)
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
# connection by specifying a connection identifier, each connecting string

print(''.join(date1))
print('/'.join(date1))
print('~~'.join('hello'))
1
2
3
-------------练习--------------

# Millet written exercises: Given a sentence (only letters and spaces, words, separated by a space) the position of words in a sentence reversed

The while True #:
# STR = INPUT ( 'Please input sentence:')
# STR IF == 'Exit':
# Exit ()
# the else:
# = str1 str.split ( '')
# Print ( '' .join ( str1 [:: -. 1]))
. 1
2
. 3
. 4
. 5
. 6
. 7
------------- practice ---------------

# Design process: to help students practice addition within 10
#detail: randomly generated addition problem, students view the subject and enter the answer, judgment, withdraw statistics the total number of respondents, the number of correct, correct rate

Import Random #
# 0 = A
# B = 0
# the while True:
# STR = INPUT ( 'answer: or n-Y')
# STR IF == 'n-':
# Print ( 'the total number of answer% d, the correct number of % d, the correct rate .2f% '% (A, B, B / A))
# Exit ()
# the else:
# nmb1 the random.randint = (0, 10)
# nmb2 the random.randint = (0, 10)
# Jug = nmb1 + nmb2
# Print ( '% D +% D ='% (nmb1, nmb2), End = '')
# ANS = int (INPUT ())
# IF ANS == Jug:
# B + =. 1
# print ( 'correct')
# the else:
# print ( 'error')
# + A. 1 =
#
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
17
18
19
20
21
------------ ------------- practice

Program Design #: arithmetic practice within one hundred
#: Provides 10 ± * / subject, the practitioner inputs the answer, the program is automatically determined and displayed

import random
opt = ['+', '-', '*', '/']
print('开始答题:')
for i in range(10):
nmb1 = random.randint(0, 100)
nmb2 = random.randint(0, 100)
f = random.choice(opt)
print('%d%s%d= ' % (nmb1, f, nmb2),end='')
ans = int(input( ))
if (f == opt[0]):
jug = nmb1 + nmb2
elif (f == opt[1]):
jug = nmb1 - nmb2
elif (f == opt[2]):
jug = nmb1 * nmb2
else:
jug = nmb1 / nmb2
if ans == jug:
print('正确')
else:
print('错误')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

 

Guess you like

Origin www.cnblogs.com/hyhy904/p/10971688.html