4.1
String-related operations
1.
Count the number of words in the input string
,
Separate words with spaces. It works as shown in the following figure.

s=input('Please enter a string:') print('The total number of words is:',s.count(' ')+1)
2.
Write a program
,
Give a string
,
Put the characters in it
"E"
Replace with spaces and output.
x=input('Please enter a string:') x1=x.replace('E',' ') print(x1)
3.
Interactive input from keyboard
18
ID number of the position
,
To resemble
"2001
year
September 12
day
"
Output the date of birth of the person in form.
a=input('Please enter an 18-digit ID number:') list1=list(a) for i in range(0,18): a=list1[6] b = list1[7] c = list1[8] d = list1[9] e = list1[10] f = list1[11] g = list1[12] h = list1[13] print(a,b,c,d,'year',e,f,'month',g,h,'day')
a=input('Please enter an 18-digit ID number:') list1=list(a) print(''.join(map(str,list1[6:10:1])),'year',''.join(map(str,list1[10:12:1])),'month',''.join(map(str,list1[12:14:1])),'day') #map(function, iterable,...), returns the list. The function is applied to each element of the Iterable and the result is returned as a list. #''.join(): String manipulation function for character concatenation.
4.
String
'abcdefg'
Reverse output using functions
def daoxu(x): x.reverse() a=''.join(map(str,x)) return a s='abcdefg' h=list(s) print(daoxu(h))
def daoxu(list1): b = [] for i in range(6,-1,-1): c=list1[i] b.append(c) s=''.join(map(str,b)) return s a='abcdefq' list2=list(a) print(daoxu(list2))
5.
In our lives
,
Holiday greetings are essential
,
Please write a New Year greeting template using string formatting.
s=input('Please enter your name') print(str.format('{0}happy new year!',s))
6.
User enters a string
,
Put forward characters with even subscripts to merge into a new string A.
The odd-numbered characters are then proposed to merge into a new string
B,
Then string A
and
B
Connect and output.
a=input('Please enter a string:') b=a[0::2] c=a[1::2] print(b+c)
a=input('Please enter a string:') list1=list(a) b=len(list1) c=list1[0:b+1:2] d=list1[1:b+1:2] e=''.join(map(str,c)) f=''.join(map(str,d)) print(e+f)
a=input('Please enter a string:') list1=list(a) def oushu(x): b=len(x) c=x[0:b+1:2] e=''.join(map(str,c)) return e def jishu(y): b=len(y) d=y[1:b+1:2] f=''.join(map(str,d)) return f print(oushu(a)+jishu(a)) a=input('Please enter a string:') def oushu(x): b=x[0::2] return b def jishu(y): c=y[1::2] return c print(oushu(a)+jishu(a))
7.
Please meet the following requirements
,
Write a program. The user enters a string. Move all the letters in the string one bit backward, and place the last letter at the beginning of the character.
Finally, the new string is output.
a=input('Please enter a string:') list1=list(a) list1.reverse() b=list1.pop(0) list1.reverse() list1.insert(0,b) m=''.join(map(str,list1)) print('The new string is:',m)
8.
Be based on
input
Function that processes the input string and returns a string that replaces some characters as follows
If a letter is a capital consonant, replace the character with
"Iron"
.
If the letter is a lowercase consonant or a non-alphabetic character, no action is taken on the character
If a letter is an uppercase vowel, replace the character with
"Iron Yard"
.
If a letter is a lowercase vowel, use
"Yard"
Replace this character.