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.

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.

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.

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.

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.

Triple Quote

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

Convert to Uppercase

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

LEARN MORE  Java Data Types

Convert Uppercase

Convert Uppercase

Convert to Lowercase

To make uppercase chars use lower() function.

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.

Check Numeric

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

Check Alpha

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

Check Alphanumeric

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

Lower Case

We will check variable type with  islower() function.

Upper Case

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

Check Empty String

We will check variable type with  isspace() function.

LEARN MORE  In Python Operator Usage Tutorial with Examples

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.

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.

Splitting String

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

Replacing String

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

Capitalize

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

Capitalize

Capitalize

Count Occurrence

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

LEARN MORE  Python String Find Tutorial with Examples

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.

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.

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 Response

  1. 21/10/2019

    […] DAHA FAZLASI BİLGİ İÇİN … […]

Leave a Reply

Your email address will not be published. Required fields are marked *