Python String Variable Type

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/python-string-variable-type/
PINTEREST
PINTEREST
INSTAGRAM

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 those can be used with these string data. In this tutorial we will look 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 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 first 3 characters by giving 0:3 like below.

Access Characters

Access Characters

Update String

We can update string variable by reassigning new values.

Single Quote

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

Double Quote

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

Triple Quote

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

LEARN MORE  Python For Loop

Convert to Uppercase

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

Convert Uppercase

Convert Uppercase

Here only string variable content is returned uppercase is stay same lowercase as before.

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 have numeric characters or uppercase characters etc.

Check Numeric

Check Alpha

Check Alphanumeric

Lower Case

Upper Case

Check Empty String

Joining String

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

LEARN MORE  Brute Force Application Written In Java

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

Splitting string is easy. By default space is used as delimiter. But delimiter can be provided.

Replacing String

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

Capitalize

We can capitalize 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 .

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 Get Today's Date?

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

Split String Into Word List

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

Split String Into Word List

Split String Into Word List

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/python-string-variable-type/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)