Strings in python tutorial for beginners 2022

    By: Manu
    2 years ago

    Alright in this guide we are going to explore Python strings. how strings work, what functions we have, most used functions and so on


    String in Python

    So here is a example for string

    // Simple string
    
    "any text goes here" or "single word"
    

    To print the value we use

    // Print values
    
    // print variable value
    
    print(variable_name)
    
    // Print variable type
    
    print(type(variable_name))
    


    Define a string and assign it to variable in Python


    // Defining a first_name string and assigning to variable
    
    first_name = 'Roger'
    


    Concatenating Two strings


    first_name = 'Roger'
    last_name = ' smith'
    
    output = first_name + last_name
    
    output -> Roger smith
    


    Get user input as string in Python


    Here we get user input and assign it to variable "first_name".

    first_name = input("Please enter your name")
    print_r(first_name)
    


    Important String Functions in Python

    lower()      // makes string lowercase.
    upper()      // makes string uppercase.
    capitalize()       // capitalize string.
    
    
    islower()      // checks is string is lower case returns true or false
    isupper()      // checks is string is upper case returns true or false
    isalpha()      // checks is string is alphabet only returns true or false
    isdigit()      // checks is string is numbers only returns true or false
    isalnum()      // checks is string is has alpha or number returns true if either type is present
    
    find()        // finds substring in string returns index or -1
    index()        // finds substring in string returns index or error
    
    len()         // returns length of string
    strip()         // removes specific values from string.