Python String Variable Type – POFTUT

Python String Variable Type


The string is a type used to hold text data in Python programming language. We can hold name, surname, address, text or similar data in strings. There are different functions that can be used with these string data. In this tutorial, we will look at them in detail.

Define String Variable

Defining string is easy as just setting string value into a variable by using quotes. In this example, we create a string variable named s and set string value This is a string by using a single quote.

s='This is a  string'

Access Characters

String variables consist of characters. If we need we can access these characters like a character array. We will specify start and end index numbers like a list and we will get related characters. In this example, we will get the first 3 characters by giving 0:3 like below.

s[0:3]

s[3:4]

s[3:]
Access Characters
Access Characters

Update String

We can update a string variable by reassigning new values. In this example, we will set the string variable s value as This is a new string by using a double quote.

s="This is a new string"

Single Quote

We can use a single quote to define string variables while providing string value. Using a single quote for small symbol-like strings is the best way.

s='TR'

Double Quote

The double quote can be used in strings where interpolations and natural language messages are required. We can also use a single quote as a string part inside the double quote.

s="I'll call you when I'm available"

Triple Quote

The triple quote is most useful for docstrings and raw string literals definition. The triple quote may wrap multiple lines like below.

sss='''This 
is 
a 
string'''

Convert to Uppercase

To make whole string uppercase use the upper function of the string variable.

ss="This is poftut"
ss.upper()
Convert Uppercase
Convert Uppercase

Convert to Lowercase

To make uppercase chars use lower() function.

sl="THIS IS POFTUT"
sl.lower()
Convert Lowercase
Convert Lowercase

Detect String Type Methods

Python provides methods to detect string type. What I mean with string type is for example if the string has numeric characters or uppercase characters etc.

LEARN MORE  How To Run/Execute Python Script Tutorial with Examples?

Check Numeric

We will check the variable type with  isnumeric() function.

>>> num="8" 
>>> print(num.isnumeric()) 
True
>>> num="c" 
>>> print(num.isnumeric()) 
False

Check Alpha

We will check the variable type with  isalpha() function.

>>> a="poftut1" 
>>> print(a.isalpha()) 
False 
>>> a="poftut"         
>>> print(a.isalpha()) 
True

Check Alphanumeric

We will check the variable type with  isalnum() function.

>>> a="poftut1" 
>>> print(a.isalnum())    
True
>>> a="!"              
>>> print(a.isalnum()) 
False

Lower Case

We will check variable type with  islower() function.

>>> a="poftut" 
>>> print(a.islower()) 
True 
>>> a="Poftut"         
>>> print(a.islower()) 
False

Upper Case

We will check the variable type with  isupper() function.

>>> a="Poftut"         
>>> print(a.isupper()) 
False 
>>> a="POFTUT"         
>>> print(a.isupper()) 
True

Check Empty String

We will check variable type with  isspace() function.

>>> a="POFTUT IS"      
>>> print(a.isspace()) 
False 
>>> a="  "             
>>> print(a.isspace()) 
True

Joining String

Two strings can be joined together. The first string will be joined for each char in the second string as we will see next example.

>>> a="I love poftut" 
>>> " ".join(a) 
'I   l o v e   p o f t u t'

Reversing String

Reversing string can be done with reversed() function. But there is a tip here. Use reversed with join because reversed returns iterator which is not a string. By using it with join with null string value we will get reversed string.

>>> a="I love poftut"
>>> print("".join(reversed(a))) 
tutfop evol I

Splitting String

The splitting string is easy. By default, space is used as a delimiter. But delimiter can be provided.

>>> a.split() 
['I', 'love', 'poftut'] 
>>> a.split('o') 
['I l', 've p', 'ftut']

Replacing String

Replace function gets two parameters first is which chars will be changed, second is what will be new chars.

>>> a="I love poftut"           
>>> a.replace("poftut","POFTUT")
I love POFTUT

Capitalize

We can capitalize on the start character or first letter of the string by using capitalize function.

s="this is a string"

s.capitalize()
Capitalize
Capitalize

Count Occurrence

String variable provides count function to get the count of given characters occurrence. In this example, we will count i .

s="this is a string" 
s.count('i')
Count Occurrence
Count Occurrence

Strip Spaces

While working with strings there will be some unwanted spaces at the begging or end of the strings. We can remove the spaces easily with split function.

LEARN MORE  How To Check Whether A File Exists Using Python?

s=”  this is a string  ”
s.strip()

Strip Spaces
Strip Spaces

Split String Into Word List

Another useful function provided by a string is splitting the string into a word list. While slitting space will be used as a delimiter.

s="this is a string"     
word_list = s.split()
Split String Into Word List
Split String Into Word List

Python String Variable Type Infographic

Python String Variable Type Infographic
Python String Variable Type Infographic

 

1 thought on “Python String Variable Type”

Leave a Comment