Python Variables: How to Define/Declare String Variable Types

🚀 Smart Summary

Variables in Python are essential for storing and referencing data within a program, acting as containers for values that can be manipulated and reused across different operations.

  • Memory Allocation: Variables reserve memory locations to store values, enabling a program to process and reference data efficiently.
  • Flexible Naming: Variables can be declared using any name or alphabets, such as a, aa, or abc, as long as the naming follows Python’s rules.
  • Re-declaration: Variables can be reassigned new values at any point, allowing for dynamic updates and reusability within the code.
  • Data Type Handling: Each variable holds a specific data type; concatenating different types, like numbers and strings, requires explicit type conversion (e.g., str()) to avoid errors.
  • Global vs Local: Variables are either global (accessible throughout the program) or local (confined to a specific function), with the global keyword used to reference global variables inside functions.
  • Delete and Cleanup: Variables can be removed from memory using the del statement, making referenced names invalid and preventing further access.
  • Best Practices: Use descriptive names, avoid unnecessary re-declarations, and manage scope carefully to maintain clarity and reliability in Python code.

Python Variables

What is a Variable in Python?

A Python variable is a reserved memory location to store values. In other words, a variable in a Python program gives data to the computer for processing.

Python Variable Types

Every value in Python has a data type. Different data types in Python are Numbers, List, Tuple, Strings, Dictionary, etc. Variables in Python can be declared by any name or even letters like a, aa, abc, etc.

How to Declare and Use a Variable

Let’s see an example. We will define a variable in Python and declare it as “a” and print it.

a=100 
print (a)

How to Re-declare a Variable?

You can re-declare Python variables even after you have declared them once.

Here we have Python declare a variable initialized to f=0.

Later, we re-assign the variable f to the value “guru99”

Re-declare a Variable

Python 2 Example

# Declare a variable and initialize it
f = 0
print f
# re-declaring the variable works
f = 'guru99'
print f

Python 3 Example

# Declare a variable and initialize it
f = 0
print(f)
# re-declaring the variable works
f = 'guru99'
print(f)

Python String Concatenation and Variable

Let’s see whether you can concatenate different data types like string and number together. For example, we will concatenate “Guru” with the number “99”.

Unlike Java, which concatenates a number with a string without declaring the number as a string, while declaring variables in Python requires declaring the number as a string, otherwise it will show a TypeError

Python String Concatenation and Variable

For the following code, you will get undefined output –

a="Guru"
b = 99
print a+b

Once the integer is declared as a string, it can be concatenated with “Guru” + str(“99”)= “Guru99” in the output.

a="Guru"
b = 99
print(a+str(b))

Python Variable Types: Local & Global

There are two types of variables in Python: Global variables and Local variables. When you want to use the same variable for the rest of your program or module you declare it as a global variable, while if you want to use the variable in a specific function or method, you use a local variable while Python variable declaration.

Let’s understand these Python variable types with the difference between local and global variables in the program below.

  1. Let us define a variable in Python where the variable “f” is global in scope and is assigned the value 101, which is printed in the output
  2. Variable f is again declared in the function and assumes local scope. It is assigned the value “I am learning Python,” which is printed out as output. This Python variable declaration is different from the global variable “f” defined earlier
  3. Once the function call is over, the local variable f is destroyed. At line 12, when we again print the value of “f” is it displays the value of the global variable f=101

Python Variable Types

Python 2 Example

# Declare a variable and initialize it
f = 101
print f
# Global vs. local variables in functions
def someFunction():
# global f
    f = 'I am learning Python'
    print f
someFunction()
print f

Python 3 Example

# Declare a variable and initialize it
f = 101
print(f)
# Global vs. local variables in functions
def someFunction():
# global f
    f = 'I am learning Python'
    print(f)
someFunction()
print(f)

While Python variable declaration using the keyword global, you can reference the global variable inside a function.

  1. Variable “f” is global in scope and is assigned the value 101, which is printed in the output
  2. Variable f is declared using the keyword global. This is NOT a local variable, but the same global variable declared earlier. Hence, when we print its value, the output is 101
  3. We changed the value of “f” inside the function. Once the function call is over, the changed value of the variable “f” persists. At line 12, when we again print the value of “f” is it displays the value “changing global variable”

Python Variable Types

Python 2 Example

f = 101;
print f
# Global vs.local variables in functions
def someFunction():
  global f
  print f
  f = "changing global variable"
someFunction()
print f

Python 3 Example

f = 101;
print(f)
# Global vs.local variables in functions
def someFunction():
  global f
  print(f)
  f = "changing global variable"
someFunction()
print(f)

How to Delete a Variable?

You can also delete Python variables using the command del “variable name”.

In the example below of Python delete variable, we deleted the variable f, and when we proceed to print it, we get the error “variable name is not defined,” which means you have deleted the variable.

Delete a variable

Example of Python delete variable or Python clear variable :

f = 11;
print(f)
del f
print(f)

FAQs

A variable in Python is a named storage location used to hold data values. It acts as a reference to an object in memory. You can assign, update, or delete variables dynamically without declaring their type, since Python automatically handles data types at runtime.

An array in Python is a collection of elements stored at contiguous memory locations, allowing efficient data access and manipulation. While Python lists can behave like arrays, the array module or NumPy arrays are used for better performance and type consistency in numerical computations.

Python’s four main variable types are: Local variables (inside functions), Global variables (accessible throughout a module), Instance variables (unique to each object), and Class variables (shared across all class instances). Each serves different scopes and behaviors in Python’s object-oriented structure.

Summarize this post with: