Working with strings

String Python logo

What does Python consider a string?

In short, anything between single, double and triple quotes.

The following give the same output.


                        print('Hello World')
                        print("Hello World")
                    

                        Hello World
                        Hello World
                    

Methods

A method is a function that can be called on a data type to perform a specific task.

Basic String Methods

Python has the following methods to change the case of strings.

  • Title Case: Capitalizes the first letter in each word.
    .title()
    
                                        my_name = "eRiDanI ALCAntar"
                                        print(my_name)
                                        print(my_name.title())
                                    
                                        
                                            eRiDanI ALCAntar
                                            Eridani Alcantar
                                        
                                    
  • Upper Case: Capitalizes every letter in the string.
    .upper()
    
                                        my_name = "eridani Alcantar"
                                        print(my_name)
                                        print(my_name.upper())
                                    
                                        
                                            eridani Alcantar
                                            ERIDANI ALCANTAR
                                        
                                    
  • Lower Case: Uses lowercase for every letter in the string.
    .lower()
    
                                        message = "HELLO WORLD"
                                        print(message)
                                        print(message.lower())
                                    
    
                                        HELLO WORLD
                                        hello world
                                    
  • Right Strip: Strips the whitespace at the end of the string.
    .rstrip()
    
                                        message = "HELLO WORLD    "
                                        print(message)
                                        print(message.rstrip())
                                    
     
                                        HELLO WORLD____
                                        HELLO WORLD