GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing pythonadventures.wordpress.com — string – Python Adventures Python AdventuresWelcome to the Jungle!
Archive
Posts Tagged ‘string’
string distancesSee the Jellyfish project: “Jellyfish is a python library for doing approximate and phonetic matching of strings“. Jellyfish implements the following algorithms: Levenshtein Distance, Damerau-Levenshtein Distance, Jaro Distance, Jaro-Winkler Distance, Match Rating Approach Comparison, Hamming Distance. See the project page for more info. New string formatting syntaxI’m still using Python 2.6 but I think it’d be a good idea to start using the new string formatting syntax that was introduced in Python 3. Since it was backported to the 2.6 version, we can start using it right away. Learn more:
This post is rather a reminder for me that I should read more about this topic. Later, I’ll add some examples too. Update (20110704) I asked a question about string formatting on python-list and got lots of useful answers. Here I’d make a short summary. Old style, but still supported: "the %s is %s" % ('sky', 'blue')
New style #1: "the {0} is {1}".format('sky', 'blue')
New style #2, from Python 2.7+: "the {} is {}".format('sky', 'blue')
New style #3, very useful for long string formattings: "the {what} is {color}".format(what='sky', color='blue')
In new codes, I stopped using the old style. I use new style #1 and #3. Related posts
Categories: python
Tags: new string formatting, string
StringBuilder functionality in PythonProblem You need to concatenate lots of string elements. Under Java we use a StringBuilder for this, but how to do that in Python? Solution #1 Use a list, and join the elements of the list at the end. This is much more efficient than concatenating strings since strings are immutable objects, thus if you concatenate a string with another, the result is a NEW string object (the problem is the same with Java strings). Example: def g():
sb = []
for i in range(30):
sb.append("abcdefg"[i%7])
return ''.join(sb)
print g() # abcdefgabcdefgabcdefgabcdefgab
Solution #2 (update 20120110) Use a StringIO object and print to it. In short: from cStringIO import StringIO out = StringIO() print >>out, 'arbitrary text' # 'out' behaves like a file return out.getvalue()
Categories: python
Tags: concatenate, java, string, stringbuffer, stringbuilder, StringIO
Reverse a stringExercise #1: Take a string and reverse its characters. For instance “ab12” => “21ba”. Solution: #!/usr/bin/env python s = 'Python adventures' print s # Python adventures print s[::-1] # serutnevda nohtyP Slice notation has the form Exercise #2: Decide if a word is a palindrome. Solution: #!/usr/bin/env python
def is_palindrome(str):
return str == str[::-1]
print is_palindrome('1367631') # True
print is_palindrome('Python') # False
Blog Stats
Random PostRecent Postsascii
bash
beautifulsoup
book
bpython
challenge
command-line
convert
cron
crontab
date
datetime
distance
dropbox
email
eric
exercise
extract
file
firefox
github
google
hacking
heroku
html
ide
imdb
imgur
install
ipython
java
jinja2
json
linux
module
modules
MongoDB
movies
news
pdf
perl
pi
pip
print
pylint
pypi
python3
python shell
quote
readline
reddit
redirect
refactoring
regexp
requests
reverse
review
shell
sound
stdout
string
tab
text
threads
time
tip
tutorial
unicode
url
video
virtualenv
virtual environment
virtualenvwrapper
xml
zip Archives
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |