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

How to use a linear index to access a 2D array in Python
Tflearn Custom Objective function
Text Classification - Label Pre Process [closed]
how to get matched substring list in a json-type string with python?
Lambda Expressions: Returning Multiple Values
Using CUDA8 in theano
matplotlib.widgets Button doesn't work inside a class
fault in readline() or condition
How to set PYTHONSTARTUP script for Intellij/PyCharm
Learning Python the Hard Way (Third Edition) Exercise 11 Print statements not printed until after input is entered
Pandas read in chunks of data with variable number of rows?
Python3 Regex: Remove all characters except / and | from string
python can`t get correct page source code
Using numpy to square value gives negative number
Execute cURLs command line in Python
Python subprocess.Popen and stdin: No such file or directory

Categories

HOME
sharepoint
sql-server-2008
udp
crate
websphere
webrtc
paramiko
google-contacts
boost-thread
deep-linking
mainframe
appmaker
histogrammar
urlencode
docker-windows
structuremap
dropbear
eclipse-cdt
packer
wicket
jboss-eap-7
iso
game-maker-studio-1.4
game-physics
jacoco
yeoman-generator
factor-analysis
flexlm
linq-to-sql
attask
jive
distance
embedly
activepython
vcard
data-conversion
weinre
consumer
hotmail
reactivemongo
remote-server
pox
pace
broadleaf-commerce
autocompletetextview
swagger-editor
silverlight-3.0
intellij-idea-2016
knockout-3.0
service-fabric-stateful
matlab-cvst
rich-text-editor
worker
rapidweaver
gd
seamless-immutable
istorage
maintenance
struts-layout
crystal-reports-8.5
instruction-set
uicollectionviewlayout
pspice
typhoon
hspec
alter
windows-azure-pack
mailcatcher
firepath
gameanalytics
dbscan
inet
except
coldfusion-7
fabric-twitter
windowlistener
anti-patterns
graphical-logo
scrollspy
httpie
low-level
multipleselection
hana-xs
client-side-validation
inputaccessoryview
omnifocus
formvalidation-plugin
suffix-array
iostream
genetic-programming
crystal-reports-10
db4o
visual-studio-addins
fogbugz-api
nservicebus4
image-zoom
leptonica
jqmobi
javascriptmvc
haskell-platform
surveyor-gem
deploying
signals2
ubuntu-11.04
pantheios
requestfactory
hibernate3-maven-plugin
usergroups
dotproject
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