python


Python 3, receive a string as an argument without any return value


I'm learning Python and have been taking an online class. This class was very basic and I am know trying to continue my studies elsewhere. Stackoverflow.com has helped me a great deal. In the online course we didn't cover a lot about return statements, which I am now trying to learn. I would like to do something very basic, so I was thinking of creating a program that would receive a string as an argument without having any return value. I want the user to type a word that will be shown with characters or symbols between every letter.
Example
User types in the word Python.
The word will be shown as =P=y=t=h=o=n= or -P-y-t-h-o-n- or maybe with * between every letter.
Is this an easy task? Can someone help me how to go about doing this?
Thank you.
Joel
If you want to do it yourself, you can go through your string like this:
my_string = "Python"
for letter in my_string:
# do something with the letter
print(letter)
This will print each letter in your word. What you want to do is having a new string with your desired character. You probably know you can concatenate (append) two strings in this way :
str1 = "hello"
str2 = "world"
str3 = str1 + str2
print(str3) #helloworld
So to do what you'd like to do, you can see each letter as a substring of your main string, and your desired character (for example *) as another string, and build a result string in that way.
inputString = "Python"
result = ""
myChar = "*"
for letter in inputString:
# build your result
build = build + letter
print(build)
This will just copy inputString into result, though I think you'll have understood how to use it in order to add your custom chars between the letters.
Yes python makes this sort of string manipulation very easy (some other languages... not so much). Look up the standard join function in the python docs.
def fancy_print(s, join_char='-'):
# split string into a list of characters
letters = list(s)
# create joined string
output = join_char + join_char.join(letters) + join_char
# show it
print(output)
then
>>> fancy_print("PYTHON")
-P-Y-T-H-O-N-
>>> fancy_print("PYTHON", "*")
*P*Y*T*H*O*N*

Related Links

Launching default application for given type of file, OS X
Python CGI script IOError Broken Pipe
Django, grouping query items
Regular expression works normally, but fails when placed in an XML schema
too many threads due to synch communication
How can I specify a relative path in a Python logging config file?
Python “draw() must be called with Label instance as first argument (got _WindowMetaclass instance instead)”
Writing to a file in Python inserts null bytes
Disable logging during manage.py test?
turbogears request/user object in templates and request context
Django date filter to output “am” or “A.M.”
Schedule Python Script - Windows 7
slicing arrays in numpy/scipy
Recommendations for a simple 2D graphics python library that can output to screen and pdf?
Reading numpy arrays outside of Python
Return value from thread

Categories

HOME
arrays
json
crystal-reports
cluster-computing
pug
sass
oauth
deep-linking
mapbox-gl-js
apollo
branch.io
dataframe
subquery
eval
getorgchart
intellij-plugin
cs-cart
rebol2
datagrip
dendrogram
bar-chart
python-unicode
pycrypto
multiple-columns
thingsboard
source-maps
rworldmap
azure-servicebus-queues
non-deterministic
net-snmp
version-numbering
mangodb
websauna
red-black-tree
pari
ipfw
skygear
stocks
fractals
canvasjs
vsts-package-management
office365connectors
mongoexport
jquery-multidatespicker
rule
tango
context-free-language
fqdn
chown
uiviewpropertyanimator
bnd
fedora20
react-native-fbsdk
launch
atlassian-crowd
system.web.ui.webcontrols
pdflib
gradle-script-kotlin
mercurial-hook
clob
date-format
mpeg-4
financial
servlet-3.0
acm
anythingslider
android-cursoradapter
jta
mirrorlink
qbfc
wif
connection-reset
httpie
chaining
orientation-changes
sysctl
ember-components
korma
codeigniter-a3m
data-generation
kraken.js
logentries
windows-mobile-6
va-list
vmware-server
carddav
argb
google-closure-library
mailcore
marmalade-edk
vim-powerline
tfs-power-tools
die
law-of-demeter
shared-objects
surf
easygui
gtk2hs
adsl
port-scanning
for-xml-path
j-interop
msf
rd

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App