Python Strings (JNNC Technologies)


A string is usually a bit of text in programming that is written to be displayed to users. It is known to Python when you want to display a string. This is because programmers use either double quote  or single quote  to enclose a word or group of words to express a string.


Example:
#!/usr/bin/python

ch = 'HelloPython'

str1 = "String Chapter"

Accessing String Values

Characters are not supported by Python which makes it simpler as characters in Python are treated as strings of length 1 and hence considered as a sub-string.
Program showing the use of strings and how they are displayed on-screen.
Example:
#!/usr/bin/python

ch = 'Hello Python'

str1 = "String Chapter"

print ("First value is: " ,  ch)

print ("Second value is: " ,  str1)
Output:
First value is: Hello Python
Second value is: String Chapter
If they are considered as a list of characters, then the example shown below will let you understand how they are treated individually:
Example:
#!/usr/bin/python

ch = "Hello Python"

str1 = "String Chapter"

print ("First single sub-string is: " ,  ch[0])

print ("Set of sub-string is: " ,  str1[2:6])
Output:
First single sub-string is: H
Set of sub-string is: ring C

0 Comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();