Here’s what you’ll learn in this article: Python provides a rich set of operators, functions, and methods for working with strings. When you are finished with this tutorial, you will know how to access and extract portions of strings, and also be familiar with the methods that are available to manipulate and modify string data.
Strings can be created by enclosing characters inside a single quote or double-quotes. Even triple quotes can be used in Python but generally used to represent multiline strings and docstrings.
>>> my_string = 'Hello'
>>> print(my_string)
'Hello'
>>> my_string = "Hello"
>>> print(my_string)
'Hello'
>>> my_string = '''Hello'''
>>> print(my_string)
'Hello'
The sections below highlight the operators, methods, and functions that are available for working with strings.
+
OperatorThe +
operator concatenates strings. It returns a string consisting of the operands joined together, as shown here:
>>> s = 'Hello'
>>> t = 'World'
>>> u = '!!!'
>>> s + t
'HelloWorld'
>>> s + t + u
'HelloWorld!!!'
>>> print('Hello World' + '!!!')
Hello World!!!
*
OperatorThe *
operator creates multiple copies of a string. If s
is a string and n
is an integer, either of the following expressions returns a string consisting of n
concatenated copies of s
:
>>> s = "Hey-"
>>> s*3
'Hey-Hey-Hey-'
The multiplier operand n
must be an integer. You’d think it would be required to be a positive integer, but amusingly, it can be zero or negative, in which case the result is an empty string:
>>> s = "Hey-"
>>> s*-4
''
in
OperatorPython also provides a membership operator that can be used with strings. The in
operator returns True
if the first operand is contained within the second, and False
otherwise:
>>> s = "Hello"
>>> s in "Hello world"
True
>>> s in "Bye world"
False
There is also a not in
operator, which does the opposite:
>>> "hello" not in "hey world"
True
>>> "hello" not in "hello world"
False
As you saw in the tutorial on Basic Data Types in Python, Python provides many functions that are built-in to the interpreter and always available. Here are a few that work with strings:
Function | Description |
---|---|
chr() | Converts an integer to a character |
ord() | Converts a character to an integer |
len() | Returns the length of a string |
str() | Returns a string representation of an object |
ord()
This returns an integer value for the given character.
At the most basic level, computers store all information as numbers. To represent character data, a translation scheme is used which maps each character to its representative number.
The simplest scheme in common use is called ASCII. It covers the common Latin characters you are probably most accustomed to working with. For these characters, ord(c)
returns the ASCII value for character c
:
>>> ord('a')
97
>>> ord('A')
65
>>> ord('#')
35
>>> ord('€')
8364
>>> ord('∑')
8721
chr()
Returns a character value for the given integer.
chr()
does the reverse of ord()
. Given a numeric value n
, chr(n)
returns a string representing the character that corresponds to n
:
>>> chr(97)
'a'
>>> chr(65)
'A'
>>> chr(8721)
'∑'
len()
It will return the length of a string.
With len()
, you can check Python string length. len(s)
returns the number of characters in s
:
>>> s = 'hello world!!!'
>>> len(s)
14
str(obj)
This returns a string representation of an object.
Virtually any object in Python can be rendered as a string. str(obj)
returns the string representation of object obj
:
>>> str(49.2)
'49.2'
>>> str(3+4j)
'(3+4j)'
>>> str(3 + 29)
'32'
>>> str('hey')
'hey'
>>> str('hey'*2)
'heyhey'
Python also allows a form of indexing syntax that extracts substrings from a string, known as string slicing. If s
is a string, an expression of the form s[m:n]
returns the portion of s
starting with position m
, and up to but not including position n
:
>>> s = 'hello world!!!'
>>> s[1:5]
'ello'
>>> s[:]
'hello world!!!'
>>> s[::-1]
'!!!dlrow olleh'
>>> s[:-4]
'hello worl'
Methods in this group perform case conversion on the target string.
s.capitalize()
Capitalizes the target string.
s.capitalize()
returns a copy of s
with the first character converted to uppercase and all other characters converted to lowercase:
>>> s = 'foO BaR BAZ quX'
>>> s.capitalize()
'Foo bar baz qux'
Non-alphabetic characters are unchanged:
>>> s = 'foo123#BAR#.'
>>> s.capitalize()
'Foo123#bar#.'
s.lower()
Converts alphabetic characters to lowercase.
s.lower()
returns a copy of s
with all alphabetic characters converted to lowercase:
>>> 'FOO Bar 123 baz qUX'.lower()
'foo bar 123 baz qux'
s.swapcase()
Swaps case of alphabetic characters.
s.swapcase()
returns a copy of s
with uppercase alphabetic characters converted to lowercase and vice versa:
>>> 'FOO Bar 123 baz qUX'.swapcase()
'foo bAR 123 BAZ Qux'
Hence we studies string manipulation in python with different built in functions in python along with its implementation.
This brings the end of this Blog. We really appreciate your time.
Hope you liked it.
Do visit our page www.zigya.com/blog for more informative blogs on Data Science
Keep Reading! Cheers!
Zigya Academy
BEING RELEVANT
Through the standard form offers different advantages in mathematical calculations and scientific notation. Firstly, it…
Introduction Stress is a feeling caused by an external trigger that makes us frustrated, such…
Sociology is a broad discipline that examines societal issues. It looks at the meaningful patterns…
Some info about Inch Inches are a unique measure that persuades us that even the…
You should be familiar with logarithms to understand antilogarithms in a better manner. Logarithms involve…
यहां "नाटककार सुरेंद्र वर्मा" पुस्तक की पीडीएफ विद्यार्थी, शोधार्थी और जो इसका अभ्यास के लिए…