You can learn about Strings in Python Programs with Outputs helped you to understand the language better.
Python Programming – String Functions and Methods
Python provides built-in functions and methods that allow us to do string manipulation. Every string object that you create in python is actually an instance of string calls. The string manipulation methods that are discussed below use the following syntax:
<stringobject>.<method name>( )
len ( )
len() is a built-in function that returns the number of characters in a string.
>>> fruit = ‘ apple ‘
>>> len ( fruit )
An empty string contains no chapters and has length O, but other than that, it is the same as any other string.
A string with a length of 1 represents a chapter in python.
|a = [‘apple’, ‘banana’, ‘pomegranate’]
for x in a:
|str1 = ‘Hello World’
|Str1 = ‘Apple is healthy. Apple is also good
|str = “elephant”
count = 0
for i in str:
count = count+1
- Python Programming – Logical and Physical Line
- Python Programming – Standard Data Types
- Python Programming – Special String Operators
The method capitalize() returns a copy of the string with only its first characters capitalize.
>>> str =” this is string”
>>> str. capitalize()
‘This is string ‘
The find() method is used to locate the position of the given substring within the string; it returns -1 if it is unsuccessful in finding the substring. find(str, beg=0 end=len(string))
>>> str= “This is a simple sentence”
In other words, it returns -1 because a substring is not found in the given string. On omitting the start parameter, the function starts the searching process from the very beginning.
>>> str . find (‘ ten ‘)
>>> str. find (‘is,0,4)
>>> str. find ( ‘ is, 4,15)
It returns true if string contains only letters and dig-its, i.e., all characters are alphanumeric and false otherwise if the string contains any special character, such as _, @,#,*, including space.
>>>strl.isalnum ( )
because it has a space which is a special character.
>>>str2 = ‘Work’
>>>str2. isalnum ( )
>>>str3.isalnum ( )
>>>str4.isalnum ( )
It returns true if the string contains only letters, otherwise returns false.
>>>str4.isalpha ( )
It returns true if the string contains only digits, and false otherwise.
It converts all the uppercase letters into a string to lowercase.
>>> ‘Save Water’.lower()
It returns true if the string is in lowercase, and false otherwise.
>>>Str3 = ‘ROHAN’
It returns true if the string is in uppercase and false otherwise.
>>>str3 = ‘ ROHAN’
It returns the exact copy of the string with all letters in uppercase.
>>> ‘Save Water’.upper()
lstrip ( )
It removes all the leading (left side) whitespace in a string.
>>> str = ” Save Water”
>>> str.lstrip ( )
It removes characters from the left based on the string argument passed (including default white-space characters) and then returns a copy of the string.
>>> str=’ABC International’
>>> str.lstrip(‘ABC ‘)
It returns a copy of the string with trailing characters removed based on the string argument passed. It also removes default whitespace characters from the right.
>>> str2 = “Hello World ”
>>> str = “Television”
It returns true if the string contains only whitespace characters, and false otherwise.
>>> ‘ ‘.isspace( )
It will return false if the string contains even one character.
It checks whether the string is title-cased, i.e., the first letter of each word is capitalized. It returns true if the string is properly title-cased, and false otherwise.
>>> ‘This Is A Title’.istitle( )
>>> ‘This is A title’.istitle( )
It replaces all occurrences of the old substring in a string with the new substring.
>>> str = ‘This is just a simple string.’
>>> str.replace (‘simple’, ‘short’)
‘This is just a short string.’
>>> ‘This is a test’.replace(‘is’, ‘eez’)
‘Theez eez a test’
The replace() works like a search and replace feature of a word processing software.
It splits the string according to delimiter str (space, if not provided) and returns a list of substrings, i.e., it splits the string into a list of “words”.
>>> si = “Work is Worship”
>>> words = si.split()
[‘Work’, ‘is’, ‘Worship’]
Program to read a line and count number of words.
|#Program to read line and count number of words
my_line=input(“Enter a line: “)
for i in x:
count = count+1
Enter a line: Clean and Green Delhi
Write the output of the following program to check if “work is worship” or not. the word ‘work’ is present in the string
|str = “work is worship”
print (‘work’ in str.split ( ))
The joinQ method concatenates words from a list of words to form a single string. Elements can be joined using any separator.
>>> str1 = (‘Raja’, ‘Ram’, ‘Mohan’, ‘Rai’)
(‘Raja’, ‘Ram’, ‘Mohan’, ‘Rai’)
>>> str = ‘ ‘
‘Raja Ram Mohan Rai’
It returns a copy of the string with lowercase letters turned into uppercase and vice versa.
This function splits the strings at the first occurrence of separator and returns the string in three parts, i.e., before the separator (head), the separator itself, and the part after the separator (tail). If the separator is not found, it returns the string itself, followed by two empty strings.
>>> str = “The Golden Star”
(‘The ‘, ‘Gold’, ‘en Star’)
(‘The Golden Star’, ”, ”)
(‘Th’, ‘e’, ‘ Golden Star’)
A string is a sequence of one or more characters (letters, numbers, symbols). It is a common form of data in computer programs, and you may need to convert strings into numbers or numbers into strings. You can convert numbers into strings using the str() method. You pass either a number or a variable into the parentheses of the method, and then that numeric value will be converted into a string value. Let us first convert an integer into a string. To convert the integer 17 into a string value, you can pass 17 into the str() method:
The quotes around the number ’17’ signify that the number is no longer an integer but is now a string value. Similarly, when you pass a float into the str() method, the string value of the float will be returned. Strings can be converted to numbers by using the int() and float() methods. int() method converts the strings into integers and allows you to do calculations. You can also convert the numbers to float by using the float ( ) method.