Categories: python

String Functions in Python

Built-in String Functions: Many functions are built into the Python interpreter and are always available. You’ll see a few that work with strings and character data in this lesson:

Note: Every string method returns a new string with the modified attributes rather than altering the original string.

Following are some of the commonly used string functions:

chr():

This function converts an integer to a character

Example:

>>> chr('97')
'a'
>>> chr(35)
'#'
>>> chr(32)
' '
>>> chr(129363)
'🥓'
len():

Returns the length of a string,

Example:

>>> s = 'I am a string'
>>> len(s)
13

>>> s = ''
>>> len(s)
0
ord():

Converts a character to an integer.

Example:

>>> ord('a')
97
>>> ord(' ')
32
>>> ord('#')
35
>>> ord('€')
8364
>>> ord('∑')
8721
>>> ord('🥓')
129363
str():

Returns a string representation of an object.

For Instance,

>>> str(49.2)
'49.2'
>>> str(3 + 29)
'32'
>>> a = str(3 + 29)
>>> a
'32'
>>> type(a)
<class 'str'>
lower():

Converts all uppercase characters in a string into lowercase.

For Instance,

>>>text="HeLlo WoRld"
>>>print(text.lower())
hello world
upper():

Converts all lowercase characters in a string into uppercase.

For Instance,

>>>text="HeLlo WoRld"
>>>print(text.lower())
HELLO WORLD
title():

Convert string to title case.

Example:

>>>text="HeLlo WoRld"
>>>print(text.title())
Hello World
isupper():

Returns true if the string is in upper case, otherwise false.

Example:

>>>text="Hello world"
>>>print(text.isupper())
False
islower():

Returns true if the string is in lower case, otherwise false.

Example:

>>>text="Hello world"
>>>print(text.islower())
False

>>>text="hello world"
>>>print(text.islower())
True
find(subString):

Returns the position of any character or a subString within any given string.

For Instance,

>>>text="Write Python 3 code in this editor and run it"
>>>subtext="editor and"
>>>print(text.find(subtext))
28

Note: also read about Strings in Python

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

10 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago