Python程序提供了用于字符串连接和字符串拆分的内置函数。
split Str.split() join Str1.join(str2)
Step 1: Input a string. Step 2: here we use split method for splitting and for joining use join function. Step 3: display output.
#split of string str1=input("Enter first String with space :: ") print(str1.split()) #splits at space str2=input("Enter second String with (,) :: ") print(str2.split(',')) #splits at ',' str3=input("Enter third String with (:) :: ") print(str3.split(':')) #splits at ':' str4=input("Enter fourth String with (;) :: ") print(str4.split(';')) #splits at ';' str5=input("Enter fifth String without space :: ") print([str5[i:i+2]for i in range(0,len(str5),2)]) #splits at position 2
输出结果
Enter first String with space :: python program ['python', 'program'] Enter second String with (,) :: python, program ['python', 'program'] Enter third String with (:) :: python: program ['python', 'program'] Enter fourth String with (;) :: python; program ['python', 'program'] Enter fifth String without space :: python program ['py', 'th', 'on', 'pr', 'og', 'ra', 'm']
#string joining str1=input("Enter first String :: ") str2=input("Enter second String :: ") str=str2.join(str1) #each character of str1 is concatenated to the #front of str2 print(“AFTER JOINING OF TWO STRING ::>”,str)
输出结果
Enter first String :: AAA Enter second String :: BBB AFTER JOINING OF TWO STRING ::>ABBBABBBA