Some string method examples
This commit is contained in:
		
							parent
							
								
									7af513d8db
								
							
						
					
					
						commit
						770a1a21b6
					
				
							
								
								
									
										38
									
								
								Python/017_stringops.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								Python/017_stringops.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,38 @@
 | 
				
			||||||
 | 
					# Examples of how to do basic string operations
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					full_name = ""
 | 
				
			||||||
 | 
					loop_count = 0
 | 
				
			||||||
 | 
					# Loop here until:
 | 
				
			||||||
 | 
					#    the typed name is at least three characters in length
 | 
				
			||||||
 | 
					#    there is a space in the name between two or more words
 | 
				
			||||||
 | 
					print("Please enter your full name below.")
 | 
				
			||||||
 | 
					print("Full name must contain a first name and last name separated by a space.")
 | 
				
			||||||
 | 
					print()
 | 
				
			||||||
 | 
					while len(full_name) < 3 or full_name.find(" ") == -1:
 | 
				
			||||||
 | 
					    # If this is not the first loop iteration then print another error
 | 
				
			||||||
 | 
					    if loop_count > 0:
 | 
				
			||||||
 | 
					        print("Error! Please input a valid name!")
 | 
				
			||||||
 | 
					        print()
 | 
				
			||||||
 | 
					    # Get the full name and strip off all leading and trailing white space (spaces)
 | 
				
			||||||
 | 
					    full_name = input("   Enter your full name: ").strip()
 | 
				
			||||||
 | 
					    # Increment the loop counter
 | 
				
			||||||
 | 
					    loop_count += 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Split the name into two parts. First and last names
 | 
				
			||||||
 | 
					first_name, last_name = full_name.split(" ", 2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					print(f"Your full name is: {full_name}")
 | 
				
			||||||
 | 
					print(f"Your first name is: {first_name}")
 | 
				
			||||||
 | 
					print(f"Your last name is: {last_name}")
 | 
				
			||||||
 | 
					print()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Test how the username was entered. All upper, all lower, or mixed case
 | 
				
			||||||
 | 
					if full_name.isupper():
 | 
				
			||||||
 | 
					    print("Your name seems to have been entered in all capital letters.")
 | 
				
			||||||
 | 
					elif full_name.islower():
 | 
				
			||||||
 | 
					    print("Your name seems to have been entered in all lower case letters.")
 | 
				
			||||||
 | 
					else:
 | 
				
			||||||
 | 
					    print("Your name seems to have been entered with both capital and lower case letters.")
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user