String is a sequence of characters. This must be enclosed within single quote ('...') and double quote ("..."). Strings are immutable, indexable, iterable.
Python String Example
>>> a = "Hello" >>> b = ' World' >>> a+b 'Hello World'
The string can have any number of characters in it.
If a string contains a single quote within it, such type of string can be written in double quote.
Back slash is used with escaping quotes and other special characters.
>>> c = "I'm Happy to go" >>> print c I'm Happy to go
We can perform many operations on the strings like -
We can concatenate two or more than two strings using plus operator.
>>> str1 = "Hello John," >>> str2 = " How are you" >>> print(str1 + str2) Hello John, How are you
We can declare empty string.
str1 = '';
We can repeat a string multiple number of times, by using multiple operator.Like -
>>> str1 = "Welcome" >>> print(str1*5) WelcomeWelcomeWelcomeWelcomeWelcome
The special character escape sequence "\n" is used for newline character or line feed.
>>> x = "Hello John \n How are you?" >>> x 'Hello John \n How are you?' >>> print x Hello John How are you?
As you can see in the above example, the newline character (\n) is displayed as it is in conversational mode but when we print it, the character follows it will appear on the next line.
These are the some other escape sequences -
String Type Conversion
By using str() method, we can convert any type to string datatype.Like -
>>> a1 = 100 >>> print(type(a1)) <type 'int'> >>> str1 = str(a1) >>> print(str1) 100 >>> print(type(str1)) <type 'str'>
Python has len() method to get the length of a string.Like -
>>> str3 = "Welcome" >>> print(len(str3)) 7
Python has in-built function to iterate over string.Like -
>>> x = "Welcome" >>> for c in x: print(c) W e l c o m e >>>
We can easily check existence of a character in a string.Like -
>>> str = "Welcome" >>> "c" in str True
If 'c' character exists in string, then it will return 1 means 'Yes' otherwise it will return 0.
In python, we can easily combine two strings using join() function.Like -
>>> str1 = "4 6 7" >>> str2 = "D F G" >>> str3 = str1.join(str2) >>> print(str3) D4 6 7 4 6 7F4 6 7 4 6 7G >>>
We can split each word of a sentence by using split() function.Like -
>>> str1 = "Hello John! How are you?" >>> splt = str1.split() >>> print(splt) ['Hello', 'John!', 'How', 'are', 'you?']
Check for substring
The 'in' operator is used to check for a substrings. Like -
>>> x = 'strong' >>> 't' in x True >>> 'a' in x False >>>
String methods are used to express some operations on strings. Here is the syntax to call a method -
expr.method(arg1, arg2, ..)
These are the some mostly used Python string methods -
The ljust method is used to left justifies a string expression in a field of a given length. The general syntax is -
The exp is the expression string, n is the length of string.
Example of ljust()
>>> "Hello".ljust(6) 'Hello ' >>> "world".ljust(10) 'world '
The rjust method is used to right justifies a string expression in a field of a given length. The general syntax is -
Example of rjust()
>>> "Hello".rjust(6) ' Hello' >>> "World".rjust(10) ' World'
The center() method is used to center a string expression to the given centered length n. The general syntax is -
Example of center()
>>> x = "HELLO" >>> x.center(10) ' HELLO '
The count() method searches a given string x to see how many times string y occures in it.
Example of count()
>>> x = "elephant" >>> x.count("e") 2 >>> x.count("a") 1 >>> x.count("ele") 1
lower() and upper()
The lower() method is used to convert all strings to lowercase and upper() method is used to convert all strings to uppercase.
>>> x = "Elephant" >>> x.lower() 'elephant' >>> x.upper() 'ELEPHANT'
The find() method search for a string x within a string y.
The above method returns position where the first match begins otherwise it returns -1.
>>> x = "Elephant" >>> x.find("p") 3 >>> x.find("a") 5 >>> x.find("o") -1