Python Strings :

Python Strings :

Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes the same as double quotes. Creating strings is as simple as assigning a value to a variable. For example :

a = "Hello, World!"

Accessing Values in Strings (with methods) :

Let's try some examples for accessing a string with some basic functions :

Example :

  1. a = "Hello, World!"
  2. print(a[1]) #Get the character at position 1 (first character position 0)
  3. print(a[2:10]) #Substring, get the characters from position 2 to position 10
  4. print(a.strip()) #returns "Hello, World!"
  5. print(len(a)) #string length
  6. print(a.lower()) #string convert to lower case
  7. print(a.upper()) #string convert to upper case
  8. print(a.replace("H", "S")) #string replace
  9. print(a.split(",")) #string spliter
  10. input()

Output :

python string functions

Note : Strings can be output to screen using the print() function and remember that the first character has the position 0.

String methods :

Python includes many built-in methods but here we just adding some of the following built-in methods to manipulate strings :

  • The strip() method removes any whitespace from the beginning or the end.
  • The len() method returns the length of a string.
  • The lower() method returns the string in lower case.
  • The upper() method returns the string in upper case.
  • The replace() method replaces a string with another string.
  • The split() method splits the string into substrings if it finds instances of the separator.

String Input :

Python allows for command line input. That means we are able to ask the user for input. The following example asks for the user's name, then, by using the input() function method, the program prints the name to the screen:

Example :

  1. print("Enter your name:")
  2. name = input()
  3. print("Hello, " + name)
  4. input()

Output :

python strings input

Escape Characters :

Following table is a list of escape or non-printable characters that can be represented with backslash notation.

Backslash notation Hexadecimal character Description
\a 0x07 Bell or alert
\b 0x08 Backspace
\f 0x0c Formfeed0
\n 0x0a Newline
\r 0x0d Carriage return
\t 0x09 Tab
\v 0x0b Vertical tab

String Special Operators :

Assume string variable a = "Hello" and variable b = "Python", then :

Operator Description Example
+ Concatenation a + b will give - HelloPython
* Repetition a*2 will give - HelloHello
[] Slice a[1] will give - e
[ : ] Range Slice a[1:4] will give - ello
in Membership - if a character exists a in b will give - False
not in Membership - if a character does not exist a not in b will give - True
% Format - Performs String formatting, we learn this at next section.

String Formatting Operator :

One of Python's coolest features is the string format operator %. This operator is unique to strings and makes up for the pack of having functions from C's printf() family. Following is a simple example :

print("My name is %s and age is %d." % ('AyaN', 24))

Output Result : My name is AyaN and age is 24.

Here is the list of complete set of symbols which can be used along with % :

Format Symbol Conversion
%c character
%s string conversion
%i signed decimal integer
%d signed decimal integer
%u unsigned decimal integer
%o octal integer
%x hexadecimal integer (lowercase letters)
%X hexadecimal integer (UPPERcase letters)
%e exponential notation (with lowercase 'e')
%E exponential notation (with UPPERcase 'E')
%f floating point real number
%g the shorter of %f and %e
%G the shorter of %f and %E

Triple Quotes :

Python triple quotes comes to the rescue by allowing strings to span multiple lines, including verbatim NEWLINEs, TABs, and any other special characters.

Example :

  1. long_para="""welcome to : techbaz.org\n
    Techbaz design for Programmers, IT Students, Software and Technical posts
    lovers and we already create a(Open Source Projects)area, \t here you can
    share your Programming skills and download free license Scripts."""
  2. print(long_para)
  3. input()

Output :

strings triple quotes

Next Update

Loading...

Currently we improved our website User Interface and other contents. Our next update is adding video tutorials on this site so, We create a new YouTube channel, our YouTube channel name is CodeX.

We need some time for make video tutorials (CodeX) so, stay with us and keep supporting and you know already we make a Collaboration with HackIsOn YouTube channel long time ago So, please Subscribe Our Channels