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:
This function converts an integer to a character
Example:
>>> chr('97')
'a'
>>> chr(35)
'#'
>>> chr(32)
' '
>>> chr(129363)
'🥓'
Returns the length of a string,
Example:
>>> s = 'I am a string'
>>> len(s)
13
>>> s = ''
>>> len(s)
0
Converts a character to an integer.
Example:
>>> ord('a')
97
>>> ord(' ')
32
>>> ord('#')
35
>>> ord('€')
8364
>>> ord('∑')
8721
>>> ord('🥓')
129363
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'>
Converts all uppercase characters in a string into lowercase.
For Instance,
>>>text="HeLlo WoRld"
>>>print(text.lower())
hello world
Converts all lowercase characters in a string into uppercase.
For Instance,
>>>text="HeLlo WoRld"
>>>print(text.lower())
HELLO WORLD
Convert string to title case.
Example:
>>>text="HeLlo WoRld"
>>>print(text.title())
Hello World
Returns true if the string is in upper case, otherwise false.
Example:
>>>text="Hello world"
>>>print(text.isupper())
False
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
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
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
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…
Problem Statement (Asked By Airbnb) Given a list of integers, write a function to compute…