Strings are arrays of characters surrounded by quotation marks. Python allows both single and double quote to represent a string.
Creating a string variable:
In Python we can create a string variable just by assigning a string value to it. The following shows different ways to create a string variable.
x = "Hello Python, how are you?"
y = 'Python is fun to learn'
To create an empty string we simply write, z = ""
Accessing characters of a string:
Square brackets can be used with an index to fetch characters of a string. The index start from 0. The following code will print 'P'.
x = "Hello Python, how are you?"
print(x[6])
Getting a sub-string from a string:
To get a smaller sub-string from a longer string we use a range in square brackets. We need to specify the start and end index in the range. The sub-string is picked from the start index to one less than the end index. For example, if the range specified is 1 to 7, then the sub-string will be picked from index 1 to index 6 of the longer string.
The following code will print 'ello P' as index of 'e' is 1 and index of 'P' is 6, which is one less than 7.
x = "Hello Python, how are you?"
sub = x[1:7]
If no start index is specified, the sub-string will start from 0. The following code will print 'Hello P'.
x = "Hello Python, how are you?"
sub = x[:7]
Similarly, if no end index is specified, the sub-string will be picked from start index to last. The following code will print 'Python, how are you?'.
x = "Hello Python, how are you?"
sub = x[6:]
Finding string length:
We can use the len() function to find the length of a string. The following will print 26.
x = "Hello Python, how are you?"
print(len(x))
Trimming a string:
We can use the strip() method to remove the starting and ending spaces of a string. The following will print 'Hello there' removing the starting and ending spaces.
x = " Hello there! "
print(x.strip())
Converting a string to lower or upper case:
Using the lower() and upper() methods we can convert a string to lower and upper case.
This will print 'hello python'.
x = "Hello Python"
print(x.lower())
This will print 'HELLO PYTHON'.
x = "Hello Python"
print(x.upper())
Replacing a sub-string:
The replace() method is used to replace a sub-string with another. The following will print 'Hello Marathon'.
x = "Hello Python"
print(x.replace("Py", "Mara")
Splitting a string:
The split() method can be used to split a string into parts using a separator. The split() method returns a list of separated sub-strings. In the following example we are using comma(,) as the separator and it will print a list ['apple', 'mango', 'banana'].
x = "apple,mango,banana"
print(x.split(","))
Note: If no separator is passed to the function as an argument, it assumes space to be the separator, by default. The following code will split the string y into a list ['This', 'is', 'a', 'book'].
y = "This is a book"
print(y.split())
.
Creating a string variable:
In Python we can create a string variable just by assigning a string value to it. The following shows different ways to create a string variable.
x = "Hello Python, how are you?"
y = 'Python is fun to learn'
To create an empty string we simply write, z = ""
Accessing characters of a string:
Square brackets can be used with an index to fetch characters of a string. The index start from 0. The following code will print 'P'.
x = "Hello Python, how are you?"
print(x[6])
Getting a sub-string from a string:
To get a smaller sub-string from a longer string we use a range in square brackets. We need to specify the start and end index in the range. The sub-string is picked from the start index to one less than the end index. For example, if the range specified is 1 to 7, then the sub-string will be picked from index 1 to index 6 of the longer string.
The following code will print 'ello P' as index of 'e' is 1 and index of 'P' is 6, which is one less than 7.
x = "Hello Python, how are you?"
sub = x[1:7]
If no start index is specified, the sub-string will start from 0. The following code will print 'Hello P'.
x = "Hello Python, how are you?"
sub = x[:7]
Similarly, if no end index is specified, the sub-string will be picked from start index to last. The following code will print 'Python, how are you?'.
x = "Hello Python, how are you?"
sub = x[6:]
Finding string length:
We can use the len() function to find the length of a string. The following will print 26.
x = "Hello Python, how are you?"
print(len(x))
Trimming a string:
We can use the strip() method to remove the starting and ending spaces of a string. The following will print 'Hello there' removing the starting and ending spaces.
x = " Hello there! "
print(x.strip())
Converting a string to lower or upper case:
Using the lower() and upper() methods we can convert a string to lower and upper case.
This will print 'hello python'.
x = "Hello Python"
print(x.lower())
This will print 'HELLO PYTHON'.
x = "Hello Python"
print(x.upper())
Replacing a sub-string:
The replace() method is used to replace a sub-string with another. The following will print 'Hello Marathon'.
x = "Hello Python"
print(x.replace("Py", "Mara")
Splitting a string:
The split() method can be used to split a string into parts using a separator. The split() method returns a list of separated sub-strings. In the following example we are using comma(,) as the separator and it will print a list ['apple', 'mango', 'banana'].
x = "apple,mango,banana"
print(x.split(","))
Note: If no separator is passed to the function as an argument, it assumes space to be the separator, by default. The following code will split the string y into a list ['This', 'is', 'a', 'book'].
y = "This is a book"
print(y.split())
.
No comments:
Post a Comment