What is String in Python?
A string is a data type used in programming. It is use represent text rather than numbers.
Lets understand the concept string from an example. In Christmas lights there are small lamps, we can take Christmas lights as a String and lamps as the element of the string. All the lamps are in form of characters
We can say that string is collection of Characters.
Python defines a string variable in single quotation marks or double quotation marks.Lets define a String.
s = "Hello, World" print(s) Output: Hello, World
Access String character by index:
print(s[0]) Output:'H' print(s[8]) Output:'o' print(s[-1]) Output:'d' print(s[-3]) Output: 'r'
Concatenation & Repetition
A plus ‘+’ sign is used as the concatenation of strings. Means merging 2 or more strings.
s1 = "Mark" s2 = "Ronson" print(s1+s2) print(s1+" "+s2) Output: MarkRonson Mark Ronson
The ‘*’ concatenating multiple copies of the same string.
"Hello"*3 Output: 'HelloHelloHello'
String Slices
Python allows slicing the string by defining the start index and end index in square [] brackets separated by a colon.
print(s[0:6]) #note that[start:end] but end is exclusive Output: 'Hello,' print(s[2:8]) Output: 'llo, W' print(s[:4]) Output: 'Hell' print(s[2:]) Output: 'llo, World' print(s[-5:-1]) Output: 'Worl'
Membership Operator
Check the word exist in the string or not using ‘in’ and ‘not in’ keyword. These are python membership operator, Use to check if a value exists in sequence.
x = "Hello world" if 'Hel' in x: print('Hel exist in string x') Output:'Hel exist in string x' if 'hel' not in x: print('hel not exist in string x') Output:'hel not exist in string x'
. . .
Python provides the various built-in function for string manipulation. Here, some string functions are illustrated with examples. These functions take a string as an argument and perform the operation on string.
len("Hello, world") #To find length of string Output: 12 len(s) Output: 12
"hello, world".capitalize() #To capitalize string Output: 'Hello, world'
"hello, world".upper() #To convert all letter into uppercase form Output: 'HELLO, WORLD'
"Hello, WORLD".lower() #To convert all letter into lowercase form Output: 'hello, world'
# Remove white spaces form start and end of the string str1 = " hello, world! " print(str1.strip()) Output: 'hello, world!'
# Is_alpha "The cost of the banana is $15.".isalpha() Output: False # Is_digit "123".isdigit() Output: True # Is_sapce " Hello".isspace() Output: false
startswith() & endswith()
# tests if the string starts or ends with the given other string "The taste of apple is good.".startswith("The") Output: True "The taste of apple is good.".endswith("great") Output: False
Find & Replace
Python’s find() function searches for the given string within s, and returns the first index where it begins or -1 if not found.
Python’s replace() function replace all occurrences of the old word with new.
"apple is as good as banana".find('as') Output: 9 "The taste of apple is good.".replace("good","great") Output: 'The taste of apple is great.'
Split & Join
Python’ split() method split the string by given delimiter. It returns the list of the substring.
Python’ join() method is opposite of the split() function. It joins the list elements by the given delimiter and returns the string.
# syntax: s.split('delim') "apple is as good as banana".split(" ") Output: ['apple', 'is', 'as', 'good', 'as', 'banana'] # syntax: 'delim'.join(list) '/'.join(['apple', 'is', 'as', 'good', 'as', 'banana']) Output: 'apple/is/as/good/as/banana'
. . .