Skip to content
-
Subscribe to our newsletter & never miss our best posts. Subscribe Now!
Technical Personnal Blog

Technical Personnal Blog

Technical Personnal Blog

Technical Personnal Blog

  • Tech
  • Blog
  • Open Source
  • VEGAS
  • CARD
  • Favorite Link
  • Shop
  • Cart
  • Checkout
  • My account
  • Tech
  • Blog
  • Open Source
  • VEGAS
  • CARD
  • Favorite Link
  • Shop
  • Cart
  • Checkout
  • My account
Close

Search

  • https://www.facebook.com/
  • https://twitter.com/
  • https://t.me/
  • https://www.instagram.com/
  • https://youtube.com/
Subscribe
BlogTech

Pythonlearn:resources-week06

By techsupport
October 7, 2016 4 Min Read
Comments Off on Pythonlearn:resources-week06

Lecture Notes: Strings

A string is a series of alphanumeric characters in a character data type. A string is defined by grouping series of character with single or double quotes. A plus sign (+) is used to concatenate multiple character strings. A numeric string is still alphanumeric characters until it has been converted to a numeric data type. An arithmetic operation against an numeric string will still cause a trace back error.

Slicing of Strings

A slice is a character or a series of character within a string. A string slice is defined by the index operator, square brackets ‘[ ]’. Indexes (or subs) are integers or numeric expression staring with 0.

MySlice = sliceofdata[beginning position:’up to’ position].

beginning position first position of the slice. The index starts with zero ‘up to’ position last position of the slice plus one or up to but not including.

Builtin String Functions

len

For our purpose returns the length the string.

.find():

    Use the .find() function when you want to know the position of a character or a sub string  within a string. 
         To find the position of the "3" in the string "0123456": 
         data="0123456"
         pos=data.find('3')
         print pos
         >3

         To find the position of "l" in "realmadrid":
         word="realmadrid"
         pos=word.find('l')
         print pos
         >3

         To find the position of 'cd' in the string 'abcdef'
         word = 'abcdef'
         pos = word.find('cd')
         print pos
         >2

       (Note: an error you may get is "Expected character buffer," which can happen if you omit the quotation marks from your string.)

.upper():

    Use the .upper() function to capitalize each character in the given string.
        data='Hello Python'
        capdata=data.upper()
        print capdata
        >HELLO PYTHON

.replace()

    Use the .replace()to search and replace a character or letter in a given string.
        data='Hello Python'
        rpld=data.replace("Python","Leo")
        print rpld
        >Hello Leo

.lower()

        Use the .lower() function to convert the sting to the lower case.
        data='Hello Python'
        capdata=data.lower()
        print capdata
        >hello python

.startswith()

        Use the .startswith() function to check if the string begin with that character or not.
        data='Hello Python'
        print data.startswith('H')
        >True

.rstrip()

        Use the .rstrip() function <without any parameters> to remove any additional white spaces
        , ' ', from the right side of the calling string.
        data1 = 'Python is cool    '
        data2 = 'Yay'
        print data1.rstrip() + data2
        >Python is coolYay      

.lstrip()

.strip() something more about .strip() method I have assign to variable var = test new line $ print var.strip() test new line

.strip also removes the new line character at the end of the string.

.startswith()

dir isn’t a method in the str type in python; dir is rather a free function that can be used with any variable/type to know what methods are supported on this variable/type.

Back to Resources

Some basic notes

To find all functions which can be used for a string, you can do following fruit = ‘banana’ dir(fruit) <— this will give you all functions which can be applied on string. Or you can just do dir(str).

Immutable: When you apply function on a string (say fruit.upper()) then a new copy of original string with modifications as per function is provided. Original string remains intact.

Additional Notes: a better (?) way to join strings with variables

Python offers a cleaner way to join variables inside strings. Follow the example

name = “Karl”

age = “30”

print name + ” is ” + age + ” years old”

could be replaced with the much simpler (method called string interpolation)

print “%s is %d years old” % (name, age) — % is an operator that will allow us to replace the %s with a literal or the value of a variable

Above syntax gives an "TypeError: %d format: a number is required, not str"
print "%s is %d years old" % (name, int(age)) --Added int conversion

Since Python 2.6 (and including Python 3), an interpolation method named #format is available.

sample text: “This line of {} is being used to demonstrate the #format {}.”.format(‘text’, ‘method’)

yields: “this line of text is being used to demonstrate the #format method.

read more here: https://docs.python.org/2/library/string.html#format-string-syntax

This method is powerful, flexible, and intuitive.

Can we say that “join strings with variables” is a concatenation?

I think this is a better way to concatenate variables of different data types all at the same time. Like for example in this case we are concatenating name with age and if age was a decimal value it would still work fine.

name = ‘john’

age = 36

print “%s is %d years old” %(name,age)

Chapter 6 Exercises

The above page is intended as a place for students to work out solutions and answers to the exercises from the textbook. Please do not post answers to exercises that are actual graded assignments.

More Resource Topics

This is a nice page, listing all the methods for String manipulation in Python – https://docs.python.org/release/2.5.2/lib/string-methods.html

N.B. https://docs.python.org/release/2.7.10/library/stdtypes.html#string-methods is a more recent and up-to-date page for string methods

Python 2.7.8 String Services – https://docs.python.org/2/library/strings.html

Add resources for this chapter to this page..

Have a look at this section of the Python 2.7.8 documentation:- https://docs.python.org/2/library/stdtypes.html#string-methods

Author

techsupport

Follow Me
Other Articles
Previous

Pythonlearn:resources-week05

Next

Pythonlearn:resources-week07

Categories

  • Android
  • Blog
  • Favorite Link
  • linux
  • Open Source
  • opencart
  • social
  • Tech
  • Uncategorized

Archives

  • January 2025
  • March 2024
  • August 2023
  • March 2023
  • February 2023
  • November 2021
  • August 2021
  • April 2021
  • March 2021
  • December 2020
  • October 2020
  • September 2020
  • July 2020
  • June 2020
  • May 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • September 2019
  • August 2019
  • June 2019
  • October 2018
  • August 2018
  • May 2018
  • April 2018
  • March 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • June 2017
  • March 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • January 2016
  • December 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
Copyright 2026 — Technical Personnal Blog. All rights reserved. Blogsy WordPress Theme