Little Turtle Python 13th lecture after--014 string

Source: Internet
Author: User

Methods and comments for strings
Capitalize ()
Change the first character of a string to uppercase
Casefold ()
Change all characters of the entire string to lowercase
Center (width)
Centers the string and fills the new string with a space of length width
Count (Sub[,start[,end]])
Returns the number of occurrences of a sub within a string, with the start and end parameters representing the range, optional.
Encode (encoding= ' utf-8 ', errors= ' strict ')
Encodes a string in the encoding format specified by encoding.
EndsWith (Sub[,start[,end])
Checks whether the string ends with a sub substring, or returns False if True. The start and end parameters represent ranges and are optional.
Expandtabs ([tabsize=8])
Converts the tab symbol (\ t) in a string to a space, such as not specifying a parameter, and the default number of spaces is tabsize=8.
Find (Sub[,start[,end])
Detects if the sub is contained in a string, returns the index value if any, otherwise returns the -1,start and end parameters to indicate the range, optional.
Index (Sub[,start[,end]])
Same as the Find method, but an exception occurs if the sub is not in a string.
Isalnum ()
Returns True if the string has at least one character and all characters are letters or numbers, otherwise false is returned.
Isalpha ()
Returns True if the string has at least one character and all characters are letters, otherwise false is returned.
Isdecimal ()
Returns True if the string contains only decimal digits, otherwise false.
IsDigit ()
Returns True if the string contains only a number, otherwise false.
Islower ()
Returns True if the string contains at least one case-sensitive character, and the characters are lowercase, otherwise false.
IsNumeric ()
Returns True if the string contains only numeric characters, otherwise false is returned.
Isspace ()
Returns True if the string contains only spaces, otherwise false is returned.
Istitle ()
Returns True if the string is a caption (all words start with uppercase and the remaining letters are lowercase), otherwise false is returned.
Isupper ()
Returns True if the string contains at least one case-sensitive character, and the characters are uppercase, otherwise false.
Join (sub)
Inserts a string as a delimiter between all the characters in a sub.
>>> STR5 = ' FISHC ' >>> str5.join (' 12345 ') ' 1fishc2fishc3fishc4fishc5 '
Ljust (width)
Returns a left-aligned string and fills the new string with a width of length with a space.
Lower ()
Converts all uppercase characters in a string to lowercase.
Lstrip ()
Remove all spaces to the left of the string
Partition (sub)
Locate the substring sub, divide the string into a 3-tuple (pre_sub,sub,fol_sub), and return if the string does not contain a sub (' Original string ', ' ', ')
Replace (Old,new[,count])
Replaces the old substring in the string with the new substring and, if count specifies, does not exceed count times. >>> STR7 = ' I love FISHDM and seven '
>>> str7.replace (' e ', ' e ', 2) ' I love FISHDM and SEven '
RFind (Sub[,start[,end])
Similar to the Find () method, it is just looking from the right.
Rindex (Sub[,start[,end])
Similar to the index () method, but starting from the right.
Rjust (width)
Returns a right-aligned string and fills the new string with a width of length with a space.
Rpartition (sub)
Similar to the partition () method, it is just looking from the right.
Rstrip ()
Removes a space at the end of a string.
Split (Sep=none, Maxsplit=-1)
The default is a space-delimited slice string without parameters, and if the Maxsplit parameter has a setting, only the Maxsplit substring is separated, returning the list of sub-string concatenation after the slice.
>>> str7.split () [' I ', ' love ', ' fishdm ', ' and ', ' seven ']
Splitlines ([keepends])
Returns a list containing the rows as elements, separated by ' \ n ', and returns the first Keepends row if the Keepends parameter is specified.
StartsWith (Prefix[,start[,end])
Checks whether the string starts with prefix, returns True, or false. The start and end parameters allow you to specify a range check, optional.
Strip ([chars])
Remove all whitespace from the front and back of the string, the chars parameter can be customized to remove the character, optional.
Swapcase ()
Flips the case in the string.
Title ()
Returns a string that is titled (all words are started in uppercase and the remaining letters are lowercase).
Translate (table)
The characters in the string are converted according to the table's rules (which can be customized by Str.maketrans (' A ', ' B '). >>> str8 = ' aaasss sssaaa '
>>> str8.translate (Str.maketrans (' s ', ' B ')) ' aaabbb bbbaaa '
Upper ()
Converts all lowercase characters in a string to uppercase.
Zfill (width)
Returns a string of length width, the original string is right-aligned, and the front is filled with 0.
Quiz: 0. Remember how to define a string that spans multiple lines (write at least two ways to implement it)? Method One:

STR1 = "To my long hair and waist, the general return?"
This body Gentleman Yi Carefree, how material mountains and rivers rustling.
The skylight at first sight, twilight snow-white head old.
The cold sword listens to thunder, the spear alone defends the empty trench.
Drunk on the battlefield June Mo smile, one night blow to draw angle.
Jiangnan late visitors, red rope hair knot tip. ‘‘‘

Method Two:

str2 = ' long hair and waist, I will triumph back toward. \
In the past, the horse was a carefree, all young hero. \
East are good, West Lake Lakeshore. \
Armed with a bloody gun, vows to keep mountains and rivers beauty. \
Should be victorious return day, spend a night with Qing. \
I hope to die together with the son of the same robe. ‘

Method Three:

STR3 = (' long hair and waist, I will triumph back. ‘
"The former longitudinal horse as Carefree, is a young hero." ‘
' East is good, West Lake Lakeshore. ‘
' Hold the gun bloody battle, oath to keep the mountains and rivers beauty. ‘
' should be victorious return day, spend a night with Qing. ‘
' Hope to die together with the son of the robe. ‘)

1. Three quotes string usually what do we use for doing?

In the case of a triple quote string that is not assigned a value, it is usually used as a cross-line comment, for example:

2. file1 = Open (' C: \windows\temp\readme. txt ', ' r ') means "C: \windows\temp\readme" is opened as read-only. TXT "This text file, but in fact is wrong, know why?" How would you change it?

The error is due to the fact that in the string we have agreed that "\ t" and "\ R" represent "Horizontal tabs (tab)" and "carriage return" respectively, so we will not open the file according to our planned path.

Python paved the way for us to solve, just use the original string operator (R or R):

File1=open (R ' C:\windows\temp\readme.txt ', ' R ')

3. There is a string: str1 = ' <a href= ' http://www. FISHC. Com/dvd "target=" _blank "> Fish C Resource Pack </a>", how to extract substrings

STR1[16:38]

4. If you use a negative number as the index value for the Shard operation, according to the third question of the requirements you can correctly visual results?

STR1[-45:-32]

5. Or the third question of the string, what will be shown in the statement below? str1[20:-(approx.)

' FISHC '

6. It is said that only fish oil with an IQ higher than 150 can solve this string (reverting to a meaningful string): Str1 = ' i2sl54ovvvb4e3bferi32s56h; $c 43.sfc67o0cm99 '

Str1[::3]

Move hands

0. Please write a password security check script code: check.py Program Demo: Write your Own error code: Mix a piece to judge
While True:
temp = input (' Enter password ')
# a = Int (temp)
B=len (temp)
c = "The way to raise the password level:
1. The password must consist of three combinations of numbers, letters and special characters
2. The password can only start with a letter
3. Password length cannot be less than 16 bits "
d = ' [email protected]#$%^&* () _=-/,.? <>;:[]{}\| '
While Temp.isspace ():
temp = input (' re-enter password ')
Print (TEMP,B)
If B<=8 or Temp.isalnum ():
Print (' Low-level password ')
Print (c)
Elif B > Temp.isalnum () and (d in temp):
Print (' High ')
Print (' Please keep it ')
Elif b>8 or b<16 or Temp.isalnum () or (d in temp):
Print (' Medium ')
Print (c)

Method One:

str1 = "[Email protected]#$%^&* () _=-/,.? <>;:[]{}\| "
HAS_STR1 = 0
Has_num = 0
Has_alpha = 0
t = ' y '
While t = = ' Y ':
Password = input ("Enter the password combination you need to check:")
length = len (password)
while (Password.isspace () or length = = 0):
Password = input (' The password you entered is empty (or blank), please re-enter: ')
For I in Password:
If I in str1:
HAS_STR1 = 1
If I.isdigit ():
Has_num = 1
If I.isalpha ():
Has_alpha = 1
has = has_str1 + Has_num + has_alpha
If length <= 8 or Password.isalnum ():
Level = "Low"
If length > 8 and has ==2:
Level = "Medium"
If length >= + has = = 3 and Password[0].isalpha ():
Level = "High"
Print ("Your password security rating is:%s"%level)
if level = = "High":
Print ("Please keep")
Else
Print ("" To increase your password security level as follows:
1. The password must consist of three combinations of numbers, letters and special characters
2. The password can only start with a letter
3. Password length must not be less than 16 "" ")
t = input ("Do you want to test again?") ("Y" continues, other exits) ")
Note: The special number starts with a high-level password without judging

Method Two:
symbols = "[email protected]#$%^&* () _=-/,.? <>;:[]{}\| "
chars = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz '
Nums = ' 0123456789 '
t = ' y '
While t = = ' Y ':
passwd = input (' Please enter password: ')
length = Len (passwd)
#判断长度
while (Passwd.isspace () or length = = 0):
passwd = input (' The password you entered is empty (or blank), please re-enter: ')
length = Len (passwd)
If length <= 8:
Flag_len = 1
Elif 8 < length <16:
Flag_len = 2
Else
Flag_len = 3
Flag_con = 0
#判断是否包含特殊字符
For each in passwd:
If each in symbols:
Flag_con +=1
Break
#判断是否包含字母
For each in passwd:
If each in chars:
Flag_con + = 1
Break
#判断是否包含数字
For each in passwd:
If each in Nums:
Flag_con + = 1
Break
#打印结果
While 1:
Print ("Your password security rating is:", end= ")
if Flag_len = = 1 or Flag_con = = 1:
Print ("Low")
elif Flag_len = = 2 or Flag_con = = 2:
Print ("Medium")
Else
Print ("High")
Print ("Please keep")
Break
Print ("" To increase your password security level as follows:
1. The password must consist of three combinations of numbers, letters and special characters
2. The password can only start with a letter
3. Password length must not be less than 16 "" ")
Break
t = input ("Do you want to test again?") ("Y" continues, other exits) ")
Note: It is not a condition to judge an advanced password other types are advanced

Method Three:
STR0 = "" Please increase your password security level as follows:
1. Must consist of three combinations of numbers, letters and special characters (only: [Email protected]#$%^&* () _=-/,.? <>;:[]{}\|)
2. The password can only start with a letter
3. Password length cannot be less than 16 bits "" "

str1 = ' Your password security level is rated as: Low \ n ' + str0 + ' ""
4. Give Wang Nima a fruit knife to the old king next door.
5. Tell your child that he sent it under charge.
6. Summon another three little turtles, feed them mutants, and save the People of New York.
"""
str2 = ' Your password security rating is: \ n ' + str0 + ' ""
4. Help the old granny cross the road, helping her roommate mend socks
"""
STR3 = ' Your password security rating is: High \ n Please keep it up! ‘

Special = "[Email protected]#$%^&* () _=-/,.? <>;:[]{}\| "
letter = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz '
Number = ' 0123456789 '
t = ' y '

While t = = ' Y ':
Password = input ("Enter the password combination you need to check:")

# Use NUM to count the number of occurrences of a character type
num = 0
A = b = c = 0
# The input cannot be empty
while password = = ":
Password = input ("Cannot be empty, please reenter:")
# calculates the value of NUM, if the input is all characters, then num=0, so there will be <=1
If Password.isnumeric () or Password.isalpha ():
num = 1
Else
For I in Password:
If a = = 0 and I in Special:
num + = 1
A = 1
Elif b = = 0 and I in number:
num + = 1
b = 1
elif c = = 0 and I in letter:
num + = 1
c = 1
If password[0] in letter and num = = 3 and len (password) >= 16:
Print (STR3)
Elif num >= 2 and len (password) >= 8:
Print (STR2)
Elif num <= 1 and len (password) < 8:
Print (STR1)
t = input ("Do you want to test again?") ("Y" continues, other exits) ")

Little Turtle Python 13th lecture after--014 string

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.