Python Learning Fifth week string related learning content summary and homework
Week five is mainly about string-related operations, including
A sequence of characters that define a string
Basic character arithmetic
Length len (str) function
Stitching +str = str1 + ' Pinjie '
Repeat *name * 3 is the name of the string content output three times
The member operation in determines whether a string is a substring of another string
The For statement enumerates each character in a string
String Index Each string has an index value index from 0 forward or 1 to the start index operator []
The slice operation selects the subsequence syntax of the string [Start:finish]
Index value of start sub-sequence starting position
The index value of the next character in the end position of the finish sub-sequence
If you do not provide start or finish default start starts with the first character finish
As the last character.
Get inverse string [::-1]
String method: The function provided by the object
A new string, such as My_str.replace (Old,new), in which the old substring is replaced with new
Method for finding the index position of a character in a string by the Find method
Split method splits a string
Title method first letter uppercase other letter lowercase format
string comparison
Any one character corresponds to a number
Directly compare the size of the corresponding number
String formatting
Format method
Http://docs.python.org/2/library/string.html#format-string-syntax
Regular Expression Re.search ()
Https://docs.python.org/2/library/re.html
Homework after class
Write the procedure to complete the following topics 2 points
Topic content
"Pig Latin" is an English child writing rewrite game throughout the game follow the rules below
(1). The vowel letters are ' a ', ' e ', ' I ', ' o ', ' u '. The letter ' Y ' is also considered a vowel in the case of not the first letter. The other letters are supplemented with phonetic letters. For example, the word "yearly" has three vowels of ' e ', ' a ' and the last ' Y ', and three consonant letters, the first ' Y ', ' r ', and ' l '.
(2). If the English word starts with a vowel letter, add "hay" to the end of the word and get the "Pig Latin" corresponding to the word. For example, "ask" Changes to "Askhay" "use" to "Usehay".
(3). If the English word starts with the ' Q ' Letter and there is a letter ' U ' at the end of the word, "Qu" is moved to "ay" after the words are added to the word "Pig Latin". For example, "quiet" changes to "Ietquay" "Quay" to "Ayquay".
(4). If the English word starts with a consonant letter, all consecutive consonants are moved to the end of the word by adding "ay" to the word "Pig Latin". For example, "tomato" changes to "Omatotay" "School" to "Oolschay" "You" to "Ouyay" "My" to "Ymay" "Ssssh" into "Sssshay".
(5). If there are uppercase letters in the English word, all letters must be converted to lowercase.
Input format:
A series of word words are separated by a space.
Output format
Convert each word to a word by using the above rules separated by a space.
Input sample
Welcome to the Python world is ready
Output sample
Elcomeway otay ethay ythonpay orldway arehay Ouyay Eadyray
Time limit 500ms Memory limit 32000kb
Reference Code
Personal Code of Completion
OPS people write the program thinking without the developer so abstract also need to work harder.
new_words= "output_words=" #Input_Words = []input_words = raw_input (). Split () For word in input_words: word = word.lower () #print Word if Word[0] in ' Aeiou ': new_words = Word + ' Hay ' elif word[0:2] == ' qu ': new_words = word[2:len (Word)] + ' Quay ' else: tmp_chars = Word[0] #print ' haha: ',tmp_chars for char in word[1:len (Word)]: if Char not in ' Aeiouy ': &nbsP; tmp_ chars =tmp_chars + char else: break n= Len (tmp_chars) if n == len (Word): new_words = Word + ' ay ' else: new_words = word[n:len (Word)]+word[0:n]+ ' ay ' if len ( Output_words) == 0: output_words = output_words + new_ words else: output_words = output_words + ' ' + new_words Print output_words
Work done after the Internet with people on the time of the development of a classmate to give reference results
Personally think this code to write a more professional first think about the structure of the program and then implement the code is not as I understand the meaning of the surface and write code many times before finally pass the code test. This topic is mainly used in this week to learn the character split split usage string concatenation also is the use of characters in the string in.
Input_string = raw_input () string_wds=[]string_wds =input_string.split () print string_wdsdef re_index ( word ): indx = 0 for i in word: if indx == 0 and ( i not in ' Aeiou '): indx = indx + 1 else: if indx >0 and ( i not in ' Aeiouy '): indx = indx + 1 else: break return indxnew_string= ' final_string= ' for i in string_wds: i = i.lower () if i[0] in ' Aeiou ': new_string = i+ ' Hay ' else: if i[0] == ' q ' and i[1] == ' u ': new_string = i[2:len (i)]+ ' qu ' + ' ay ' else: new_string =i[re_index (i): Len (i)]+i[0:re_index (i)]+ ' ay ' if len ( final_string) == 0: final_string=final_string+new_string else: final_string=final_string+ ' ' + new_string print final_string
2. Write the procedure to complete the following topics 1 points
Topic content
Determines, in turn, whether a series of given strings is a valid Python identifier.
Input format:
A series of strings with each string in one row.
Output format
Determines whether each line of string is a valid Python identifier if it is valid output true otherwise the output is False.
Input sample
Abc
_def
21gh
Output sample
True
True
False
Time limit 500ms Memory limit 32000kb
At first I couldn't figure out how to implement multi-line input feel this for just learn the person is an unknown knowledge point Baidu then found the relevant instructions to complete this problem the most critical input requirements. Kung Fu is extracurricular. This topic is used in this week's lesson on the use of the regular matching of strings the entire implementation process is very simple.
Import Restopword = "str = ' for line in ITER" (Raw_input, Stopword): str + = line + ' \ n ' for words in Str.split (): If no T re.search (U ' ^[_a-za-z0-9]+$ ', words): print False elif not re.search (U ' ^[_a-za-z]+$ ', Words[0]): Print Fa LSE Else:print True
3 Write the procedure to complete the following topics 2 points
Topic content
Computes the alphabetic value of a series of a given string to the number value A for each letter in the string, corresponding to 1 B for 2, and so on, with a value of 0 for the case-insensitive, non-alphabetic character. For example Colin has a letter value of 3 + 15 + 12 + 9 + 14 = 53
Input format:
A series of strings with each string in one row.
Output format
Calculates and outputs the alphabetic value of each line of string.
Input sample
Colin
Abc
Output sample
53
6
Time limit 500ms Memory limit 32000kb
Personal feeling this problem is relatively simple personal use a super-outline method to get the ASCII value by a string instead of the number value. There may be a simpler way to look at the answers the teachers have written and expect better answers.
import restopword = ' str = ' For line in iter (raw_input, Stopword): str += line + ' \ n ' for words in str.split (' \ n '): if len (words) > 0 : Count = 0 words = words.lower () for c in words: if not re.search (' [A-z] ', c): num = 0 else: num = ord (c) - ord (' a ') + 1 #print "Num is", num count += num print count
Because there's something in the middle, and the first one's got a problem. The second Test set is not clear what the teacher used to conduct the test has not been through the work of this job across the December 30, 1-January 4 to complete.
This article from "Ops said: from rookie to veteran" blog, please be sure to keep this source http://liuqunying.blog.51cto.com/3984207/1599016
Python Introductory fifth week: strings and jobs